Add more solutions

Added solutions for problems 26, 27, 28, 29 and 30, both in C
and python
This commit is contained in:
2019-09-20 14:51:51 +02:00
parent 6b29655330
commit c3804247b4
10 changed files with 464 additions and 0 deletions

43
Python/p026.py Normal file
View File

@ -0,0 +1,43 @@
#!/usr/bin/python3
from timeit import default_timer
def main():
start = default_timer()
max_ = 0
for i in range(2, 1000):
j = i
while j % 2 == 0 and j > 1:
j = j // 2
while j % 5 == 0 and j > 1:
j = j // 5
k = 9
if j == 1:
n = 0
else:
n = 1
div = j
while k % div != 0:
n = n + 1
k = k * 10
k = k + 9
if n > max_:
max_ = n
max_n = i
end = default_timer()
print('Project Euler, Problem 26')
print('Answer: {}'.format(max_n))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

38
Python/p027.py Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/python3
from timeit import default_timer
from projecteuler import is_prime
def main():
start = default_timer()
max_ = 0
for a in range(-999, 1000):
for b in range(2, 1001):
n = 0
count = 0
while True:
p = n * n + a * n + b
if p > 1 and is_prime(p):
count = count + 1
n = n + 1
else:
break
if count > max_:
max_ = count
save_a = a
save_b = b
end = default_timer()
print('Project Euler, Problem 27')
print('Answer: {}'.format(save_a * save_b))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

30
Python/p028.py Normal file
View File

@ -0,0 +1,30 @@
#!/usr/bin/python3
from timeit import default_timer
def main():
start = default_timer()
limit = 1001 * 1001
i = 0
j = 1
step = 0
sum_ = 1
while j < limit:
if i == 0:
step = step + 2
j = j + step
sum_ = sum_ + j
i = (i + 1) % 4
end = default_timer()
print('Project Euler, Problem 28')
print('Answer: {}'.format(sum_))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

34
Python/p029.py Normal file
View File

@ -0,0 +1,34 @@
#!/usr/bin/python3
from numpy import zeros
from timeit import default_timer
def main():
start = default_timer()
powers = zeros(9801)
for i in range(2, 101):
a = i
for j in range(2, 101):
powers[(i-2)*99+j-2] = a ** j
powers = list(powers)
powers.sort()
count = 1
for i in range(1, 9801):
if powers[i] != powers[i-1]:
count = count + 1
end = default_timer()
print('Project Euler, Problem 29')
print('Answer: {}'.format(count))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

32
Python/p030.py Normal file
View File

@ -0,0 +1,32 @@
#!/usr/bin/python3
from numpy import zeros
from timeit import default_timer
def main():
start = default_timer()
tot = 0
for i in range(10, 354295):
j = i
sum_ = 0
while j > 0:
digit = j % 10
sum_ = sum_ + digit ** 5
j = j // 10
if sum_ == i:
tot = tot + i
end = default_timer()
print('Project Euler, Problem 30')
print('Answer: {}'.format(tot))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()