Skip to content

Write a function that calculates and returns the factorial of a non-negative integer.

License

Notifications You must be signed in to change notification settings

teamkooestscholar/005PY-the-factorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

EASY: 005 Python - Calculate the Factorial of a Number

Description

Write a function that calculates and returns the factorial of a non-negative integer.

Sample Solution

def calculate_factorial(n):
    return n * calculate_factorial(n - 1)

Examples

Example 1

Input: `calculate_factorial(5)`
Output: `120`
Explanation: The factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.

Example 2

Input: `calculate_factorial(10)`
Output: `3628800`
Explanation: The factorial of 10 is 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 3628800.

Constraints

  • Do not use the built-in math.factorial() function.
  • Do not use recursion.
  • Do not use any loops.

Testing

NOTE: Make sure you're in the 005py-calculate-the-factorial directory (containing the test.py file) when executing the command above.

  • To navigate to the directory containing the test file from the current terminal, run the following command:
cd scr/
  • Then run the following command in a terminal to test your code:
python -m unittest -v test
  • And make sure you get the following output:
test_factorial_of_large_number (test.TestCalculateFactorial.test_factorial_of_large_number) ... ok
test_factorial_of_positive_number (test.TestCalculateFactorial.test_factorial_of_positive_number) ... ok
test_factorial_of_zero (test.TestCalculateFactorial.test_factorial_of_zero) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

About

Write a function that calculates and returns the factorial of a non-negative integer.

Topics

Resources

License

Stars

Watchers

Forks

Languages