Skip to content

Commit d40bc28

Browse files
authored
Create Going to zero or to infinity?.py
1 parent f933d53 commit d40bc28

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Consider the following numbers (where n! is factorial(n)):
3+
u1 = (1 / 1!) * (1!)
4+
u2 = (1 / 2!) * (1! + 2!)
5+
u3 = (1 / 3!) * (1! + 2! + 3!)
6+
...
7+
un = (1 / n!) * (1! + 2! + 3! + ... + n!)
8+
Which will win: 1 / n! or (1! + 2! + 3! + ... + n!)?
9+
Are these numbers going to 0 because of 1/n! or to infinity due to the sum of factorials or to another number?
10+
Task
11+
Calculate (1 / n!) * (1! + 2! + 3! + ... + n!) for a given n, where n is an integer greater or equal to 1.
12+
To avoid discussions about rounding, return the result truncated to 6 decimal places, for example:
13+
1.0000989217538616 will be truncated to 1.000098
14+
1.2125000000000001 will be truncated to 1.2125
15+
Remark
16+
Keep in mind that factorials grow rather rapidly, and you need to handle large inputs.
17+
Hint
18+
You could try to simplify the expression.
19+
"""
20+
21+
import math
22+
23+
def truncate(n, decimals=0):
24+
multiplier = 10 ** decimals
25+
return int(n * multiplier) / multiplier
26+
27+
def going(n):
28+
try:
29+
count = -1
30+
for i in range(0,n+1):
31+
count += math.factorial(i)
32+
return truncate((1/math.factorial(n))*count,6)
33+
except OverflowError:
34+
exit()

0 commit comments

Comments
 (0)