MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / factorial

Function factorial

maths/primelib.py:767–794  ·  view source on GitHub ↗

input: positive integer 'n' returns the factorial of 'n' (n!) >>> factorial(0) 1 >>> factorial(20) 2432902008176640000 >>> factorial(-1) Traceback (most recent call last): ... AssertionError: 'n' must been a int and >= 0 >>> factorial("test") Tra

(n)

Source from the content-addressed store, hash-verified

765
766
767def factorial(n):
768 """
769 input: positive integer 'n'
770 returns the factorial of 'n' (n!)
771
772 >>> factorial(0)
773 1
774 >>> factorial(20)
775 2432902008176640000
776 >>> factorial(-1)
777 Traceback (most recent call last):
778 ...
779 AssertionError: 'n' must been a int and >= 0
780 >>> factorial("test")
781 Traceback (most recent call last):
782 ...
783 AssertionError: 'n' must been a int and >= 0
784 """
785
786 # precondition
787 assert isinstance(n, int) and (n >= 0), "'n' must been a int and >= 0"
788
789 ans = 1 # this will be return.
790
791 for factor in range(1, n + 1):
792 ans *= factor
793
794 return ans
795
796
797# -------------------------------------------------------------------

Callers 13

binomial_distributionFunction · 0.90
piFunction · 0.90
sinFunction · 0.90
differentiateFunction · 0.90
maclaurin_sinFunction · 0.90
maclaurin_cosFunction · 0.90
combinationsFunction · 0.90
sol2.pyFile · 0.90
solutionFunction · 0.90
solutionFunction · 0.90
solutionFunction · 0.90
sol1.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected