Add more python solutions
Added solutions for problems 16, 17 and 18
This commit is contained in:
parent
3b597a8845
commit
7d6b55e9cf
23
Python/p016.py
Normal file
23
Python/p016.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from timeit import default_timer
|
||||||
|
|
||||||
|
def main():
|
||||||
|
start = default_timer()
|
||||||
|
|
||||||
|
res = str(2 ** 1000)
|
||||||
|
|
||||||
|
sum_ = 0
|
||||||
|
|
||||||
|
for i in range(len(res)):
|
||||||
|
sum_ = sum_ + int(res[i])
|
||||||
|
|
||||||
|
end = default_timer()
|
||||||
|
|
||||||
|
print('Project Euler, Problem 16')
|
||||||
|
print('Answer: {}'.format(sum_))
|
||||||
|
|
||||||
|
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
40
Python/p017.py
Normal file
40
Python/p017.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from timeit import default_timer
|
||||||
|
|
||||||
|
def main():
|
||||||
|
start = default_timer()
|
||||||
|
|
||||||
|
n_letters = [[3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8],\
|
||||||
|
[60, 60, 50, 50, 50, 70, 60, 60],\
|
||||||
|
[1300, 1300, 1500, 1400, 1400, 1300, 1500, 1500, 1400],\
|
||||||
|
[11]]
|
||||||
|
|
||||||
|
sum_ = 0
|
||||||
|
|
||||||
|
for i in range(19):
|
||||||
|
sum_ = sum_ + n_letters[0][i]
|
||||||
|
|
||||||
|
for i in range(8):
|
||||||
|
for j in range(9):
|
||||||
|
n_letters[1][i] = n_letters[1][i] + n_letters[0][j]
|
||||||
|
sum_ = sum_ + n_letters[1][i]
|
||||||
|
|
||||||
|
for i in range(9):
|
||||||
|
for j in range(19):
|
||||||
|
n_letters[2][i] = n_letters[2][i] + n_letters[0][j]
|
||||||
|
for j in range(8):
|
||||||
|
n_letters[2][i] = n_letters[2][i] + n_letters[1][j]
|
||||||
|
sum_ = sum_ + n_letters[2][i] - 3
|
||||||
|
|
||||||
|
sum_ = sum_ + n_letters[3][0]
|
||||||
|
|
||||||
|
end = default_timer()
|
||||||
|
|
||||||
|
print('Project Euler, Problem 17')
|
||||||
|
print('Answer: {}'.format(sum_))
|
||||||
|
|
||||||
|
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
48
Python/p018.py
Normal file
48
Python/p018.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from timeit import default_timer
|
||||||
|
|
||||||
|
def sum_triangle(triang, n, i, j, sum_):
|
||||||
|
global max_
|
||||||
|
|
||||||
|
if i == n:
|
||||||
|
if sum_ > max_:
|
||||||
|
max_ = sum_
|
||||||
|
return max_
|
||||||
|
|
||||||
|
sum_triangle(triang, n, i+1, j, sum_+triang[i][j])
|
||||||
|
sum_triangle(triang, n, i+1, j+1, sum_+triang[i][j])
|
||||||
|
|
||||||
|
def main():
|
||||||
|
global max_
|
||||||
|
|
||||||
|
start = default_timer()
|
||||||
|
|
||||||
|
try:
|
||||||
|
fp = open('triang.txt', 'r')
|
||||||
|
except:
|
||||||
|
print('Error while opening file {}'.format('triang.txt'))
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
triang = list()
|
||||||
|
|
||||||
|
for line in fp:
|
||||||
|
triang.append(line.strip('\n').split())
|
||||||
|
|
||||||
|
fp.close()
|
||||||
|
|
||||||
|
for i in range(len(triang)):
|
||||||
|
triang[i] = list(map(int, triang[i]))
|
||||||
|
|
||||||
|
max_ = 0
|
||||||
|
sum_triangle(triang, 15, 0, 0, 0)
|
||||||
|
|
||||||
|
end = default_timer()
|
||||||
|
|
||||||
|
print('Project Euler, Problem 18')
|
||||||
|
print('Answer: {}'.format(max_))
|
||||||
|
|
||||||
|
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
15
Python/triang.txt
Normal file
15
Python/triang.txt
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
75
|
||||||
|
95 64
|
||||||
|
17 47 82
|
||||||
|
18 35 87 10
|
||||||
|
20 04 82 47 65
|
||||||
|
19 01 23 75 03 34
|
||||||
|
88 02 77 73 07 63 67
|
||||||
|
99 65 04 28 06 16 70 92
|
||||||
|
41 41 26 56 83 40 80 70 33
|
||||||
|
41 48 72 33 47 32 37 16 94 29
|
||||||
|
53 71 44 65 25 43 91 52 97 51 14
|
||||||
|
70 11 33 28 77 73 17 78 39 68 17 57
|
||||||
|
91 71 52 38 17 14 91 43 58 50 27 29 48
|
||||||
|
63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
||||||
|
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
Loading…
x
Reference in New Issue
Block a user