Problem 186712 · easy · Phase 01 Prerequisites & Setup

Factorial

for loop · range · accumulator

The factorial of a whole number n, written n!, is the product of all the whole numbers from 1 up to n. For example 5! = 1 * 2 * 3 * 4 * 5 = 120. By definition, 0! = 1.

Write a function factorial(n) that returns n!. Use a loop rather than a library function.

Examples

Input:  n = 5
Output: 120
Input:  n = 0
Output: 1

Constraints

  • 0 <= n <= 15

Goals

  • Loop over a sequence of numbers with for and range()
  • Know that range(a, b) stops just before b
  • Build up a result step by step in an accumulator variable
Starting Python…