File tree 2 files changed +23
-32
lines changed
2 files changed +23
-32
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
+ Problem 15: https://projecteuler.net/problem=15
3
+
2
4
Starting in the top left corner of a 2×2 grid, and only being able to move to
3
5
the right and down, there are exactly 6 routes to the bottom right corner.
4
6
How many such routes are there through a 20×20 grid?
5
7
"""
6
8
from math import factorial
7
9
8
10
9
- def lattice_paths ( n ) :
11
+ def solution ( n : int = 20 ) -> int :
10
12
"""
11
- Returns the number of paths possible in a n x n grid starting at top left
12
- corner going to bottom right corner and being able to move right and down
13
- only.
14
-
15
- bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 50
16
- 1.008913445455642e+29
17
- bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 25
18
- 126410606437752.0
19
- bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 23
20
- 8233430727600.0
21
- bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 15
22
- 155117520.0
23
- bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 1
24
- 2.0
25
-
26
- >>> lattice_paths(25)
27
- 126410606437752
28
- >>> lattice_paths(23)
29
- 8233430727600
30
- >>> lattice_paths(20)
31
- 137846528820
32
- >>> lattice_paths(15)
33
- 155117520
34
- >>> lattice_paths(1)
35
- 2
36
-
13
+ Returns the number of paths possible in a n x n grid starting at top left
14
+ corner going to bottom right corner and being able to move right and down
15
+ only.
16
+ >>> solution(25)
17
+ 126410606437752
18
+ >>> solution(23)
19
+ 8233430727600
20
+ >>> solution(20)
21
+ 137846528820
22
+ >>> solution(15)
23
+ 155117520
24
+ >>> solution(1)
25
+ 2
37
26
"""
38
27
n = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1,
39
28
# 2, 3,...
@@ -46,10 +35,10 @@ def lattice_paths(n):
46
35
import sys
47
36
48
37
if len (sys .argv ) == 1 :
49
- print (lattice_paths (20 ))
38
+ print (solution (20 ))
50
39
else :
51
40
try :
52
41
n = int (sys .argv [1 ])
53
- print (lattice_paths (n ))
42
+ print (solution (n ))
54
43
except ValueError :
55
44
print ("Invalid entry - please enter a number." )
Original file line number Diff line number Diff line change 1
1
"""
2
+ Problem 34: https://projecteuler.net/problem=34
3
+
2
4
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
3
5
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
4
6
Note: As 1! = 1 and 2! = 2 are not sums they are not included.
@@ -18,17 +20,17 @@ def sum_of_digit_factorial(n: int) -> int:
18
20
return sum (factorial (int (char )) for char in str (n ))
19
21
20
22
21
- def compute () -> int :
23
+ def solution () -> int :
22
24
"""
23
25
Returns the sum of all numbers whose
24
26
sum of the factorials of all digits
25
27
add up to the number itself.
26
- >>> compute ()
28
+ >>> solution ()
27
29
40730
28
30
"""
29
31
limit = 7 * factorial (9 ) + 1
30
32
return sum (i for i in range (3 , limit ) if sum_of_digit_factorial (i ) == i )
31
33
32
34
33
35
if __name__ == "__main__" :
34
- print (f"{ compute ()} = " )
36
+ print (f"{ solution ()} = " )
You can’t perform that action at this time.
0 commit comments