10 examples of 'factorial in python' in Python

Every line of 'factorial in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
13def factorial():
14 """
15 This function reads a number and computes its factorial
16 """
17 print("Problem: Factorial")
18
19 number = int(input())
20 result = 1
21
22 for n in range(2, number+1):
23 result *= n
24
25 print(result)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

2def factorial(n):
3 fact = 1
4 for i in range(1,n+1):
5 fact *= i
6 return fact
1def factorial(n):
2 res = 1
3 for i in range(2, n + 1):
4 res *= i
5 return res
9def factorial(n):
10 if n is 0:
11 return 1
12 else:
13 return n * factorial(n-1)
42def factorial(n):
43 """
44 normal recursive two_sum
45 :return: int
46 """
47 if n == 1 or n == 0: # base case for n = 0, 1
48 return 1
49 else: # recursive case when n > 1
50 return n * factorial(n - 1)
2def factorial(n):
3 if(n==1):
4 return 1
5 else:
6 return(factorial(n-1)*n)
11def factorial(n):
12 res = i = 1
13 while i <= n:
14 res *= i
15 i += 1
16 return res
11def factorial(n):
12 """Define the factorial for fractions and negative numbers."""
13 return gamma(n + 1)
34def inverse_factorial(number, round_up=True):
35 '''
36 Get the integer that the factorial of would be `number`.
37
38 If `number` isn't a factorial of an integer, the result will be rounded. By
39 default it'll be rounded up, but you can specify `round_up=False` to have
40 it be rounded down.
41
42 Examples:
43
44 >>> inverse_factorial(100)
45 5
46 >>> inverse_factorial(100, round_up=False)
47 4
48
49 '''
50 assert number >= 0
51 if number == 0:
52 return 0
53 elif number < 1:
54 return int(round_up) # Heh.
55 elif number == 1:
56 return 1
57 else:
58 current_number = 1
59 for multiplier in itertools.count(2):
60 current_number *= multiplier
61 if current_number == number:
62 return multiplier
63 elif current_number > number:
64 return multiplier if round_up else (multiplier - 1)
55def factorial(n):
56 return np.prod(np.arange(1, n+1))

Related snippets