Daniele Fucini cac80478da
Add more solutions
Added solutions for problems 36, 37, 38, 39 and 40 in python and C
2019-09-21 15:45:18 +02:00

61 lines
1.3 KiB
Python

#!/usr/bin/python3
from numpy import zeros
from timeit import default_timer
def main():
start = default_timer()
max_ = 0
savedc = zeros(1000, int)
for p in range(12, 1001):
count = 0
a = 0
b = 0
c = 0
m = 2
while m * m < p:
for n in range(1, m):
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if a + b + c == p and savedc[c] == 0:
savedc[c] = 1
count = count + 1
i = 2
tmpa = a
tmpb = b
tmpc = c
while tmpa + tmpb + tmpc < p:
tmpa = a * i
tmpb = b * i
tmpc = c * i
if tmpa + tmpb + tmpc == p and savedc[tmpc] == 0:
savedc[tmpc] = 1
count = count + 1
i = i + 1
m = m + 1
if count > max_:
max_ = count
res = p
end = default_timer()
print('Project Euler, Problem 39')
print('Answer: {}'.format(res))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()