8 examples of 'factorial of a number in python using for loop' in Python

Every line of 'factorial of a number in python using for loop' 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
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)
1def factorial(n):
2 res = 1
3 for i in range(2, n + 1):
4 res *= i
5 return res
2def factorial(n):
3 fact = 1
4 for i in range(1,n+1):
5 fact *= i
6 return fact
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)
9def factorial(n):
10 if n is 0:
11 return 1
12 else:
13 return n * factorial(n-1)
11def factorial(n):
12 res = i = 1
13 while i <= n:
14 res *= i
15 i += 1
16 return res
2def factorial(n):
3 if(n==1):
4 return 1
5 else:
6 return(factorial(n-1)*n)
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)

Related snippets