-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsum_of_factorial.py
69 lines (60 loc) · 1.59 KB
/
sum_of_factorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def sum_of_factorial(number: int) -> int:
"""
>>> sum_of_factorial(0)
0
>>> sum_of_factorial(1)
1
>>> sum_of_factorial(2)
3
>>> sum_of_factorial(5)
153
"""
sum_factorial = 0
factorial = 1
for i in range(1, number + 1):
factorial *= i
sum_factorial = sum_factorial + factorial
return sum_factorial
def sum_of_factorial_circle_plus_recursion(number: int) -> int:
"""
>>> sum_of_factorial_circle_plus_recursion(0)
0
>>> sum_of_factorial_circle_plus_recursion(1)
1
>>> sum_of_factorial_circle_plus_recursion(2)
3
>>> sum_of_factorial_circle_plus_recursion(5)
153
"""
sum_factorial = 0
from math import factorial
for i in range(1, number + 1):
sum_factorial += factorial(i)
return sum_factorial
def sum_of_factorial_pure_recursion(number: int) -> int:
"""
>>> sum_of_factorial_pure_recursion(0)
0
>>> sum_of_factorial_pure_recursion(1)
1
>>> sum_of_factorial_pure_recursion(2)
3
>>> sum_of_factorial_pure_recursion(5)
153
"""
if number == 1 or number == 0:
return number # 1! or 0!
elif number == 2:
return 3 # 1! + 2!
else:
return (
sum_of_factorial_pure_recursion(number - 1)
+ (
sum_of_factorial_pure_recursion(number - 1)
- sum_of_factorial_pure_recursion(number - 2)
)
* number
)
if __name__ == "__main__":
from doctest import testmod
testmod()