File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments