Add more solutions

Added solutions for problems from 41 to 5, both in C and python
This commit is contained in:
2019-09-22 17:47:09 +02:00
parent 7bab173c05
commit f8cce530b5
22 changed files with 1105 additions and 0 deletions

29
Python/p041.py Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/python3
from timeit import default_timer
from projecteuler import is_pandigital, is_prime
def count_digits(n):
i = 0
while n > 0:
i = i + 1
n = n // 10
return i
def main():
start = default_timer()
for i in range(7654321, 0, -2):
if is_pandigital(i, count_digits(i)) and is_prime(i):
print('Project Euler, Problem 41')
print('Answer: {}'.format(i))
break
end = default_timer()
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

50
Python/p042.py Normal file
View File

@ -0,0 +1,50 @@
#!/usr/bin/python3
from timeit import default_timer
def is_triang(n):
i = 1
j = 1
while j <= n:
if n == j:
return True
i = i + 1
j = j + i
return False
def main():
start = default_timer()
try:
fp = open('words.txt', 'r')
except:
print('Error while opening file {}'.format('words.txt'))
exit(1)
words = list(fp.readline().replace('"', '').split(','))
fp.close()
count = 0
for word in words:
value = 0
l = len(word)
for j in range(l):
value = value + ord(word[j]) - ord('A') + 1
if is_triang(value):
count = count + 1
end = default_timer()
print('Project Euler, Problem 42')
print('Answer: {}'.format(count))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

64
Python/p043.py Normal file
View File

@ -0,0 +1,64 @@
#!/usr/bin/python3
from itertools import permutations
from timeit import default_timer
def has_property(n):
value = int(n[1]) * 100 + int(n[2]) * 10 + int(n[3])
if value % 2 != 0:
return False
value = int(n[2]) * 100 + int(n[3]) * 10 + int(n[4])
if value % 3 != 0:
return False
value = int(n[3]) * 100 + int(n[4]) * 10 + int(n[5])
if value % 5 != 0:
return False
value = int(n[4]) * 100 + int(n[5]) * 10 + int(n[6])
if value % 7 != 0:
return False
value = int(n[5]) * 100 + int(n[6]) * 10 + int(n[7])
if value % 11 != 0:
return False
value = int(n[6]) * 100 + int(n[7]) * 10 + int(n[8])
if value % 13 != 0:
return False
value = int(n[7]) * 100 + int(n[8]) * 10 + int(n[9])
if value % 17 != 0:
return False
return True
def main():
start = default_timer()
perm = list(permutations('0123456789'))
sum_ = 0
for i in perm:
if has_property(i):
sum_ = sum_ + int(''.join(map(str, i)))
end = default_timer()
print('Project Euler, Problem 43')
print('Answer: {}'.format(sum_))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

34
Python/p044.py Normal file
View File

@ -0,0 +1,34 @@
#!/usr/bin/python3
from math import sqrt
from timeit import default_timer
from projecteuler import is_pentagonal
def main():
start = default_timer()
found = 0
n = 2
while not found:
pn = n * (3 * n - 1) // 2
for m in range(1, n):
pm = m * (3 * m - 1) // 2
if is_pentagonal(pn+pm) and is_pentagonal(pn-pm):
found = 1
break
n = n + 1
end = default_timer()
print('Project Euler, Problem 44')
print('Answer: {}'.format(pn-pm))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

29
Python/p045.py Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/python3
from math import sqrt
from timeit import default_timer
from projecteuler import is_pentagonal
def main():
start = default_timer()
found = 0
i = 143
while not found:
i = i + 1
n = i * (2 * i - 1)
if is_pentagonal(n):
found = 1
end = default_timer()
print('Project Euler, Problem 45')
print('Answer: {}'.format(n))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

52
Python/p046.py Normal file
View File

@ -0,0 +1,52 @@
#!/usr/bin/python3
from timeit import default_timer
from projecteuler import sieve
def goldbach(n):
global primes
for i in range(2, n):
if primes[i] == 1:
j = 1
while True:
tmp = i + 2 * j * j
if tmp == n:
return True
j = j + 1
if tmp >= n:
break
return False
def main():
start = default_timer()
global primes
N = 10000
primes = sieve(N)
found = 0
i = 3
while not found and i < N:
if primes[i] == 0:
if not goldbach(i):
found = 1
i = i + 2
end = default_timer()
print('Project Euler, Problem 46')
print('Answer: {}'.format(i-2))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

59
Python/p047.py Normal file
View File

@ -0,0 +1,59 @@
#!/usr/bin/python3
from timeit import default_timer
from projecteuler import sieve
def count_distinct_factors(n):
global primes
count = 0
if n % 2 == 0:
count = count + 1
while True:
n = n // 2
if n % 2 != 0:
break
i = 3
while n > 1:
if primes[i] == 1 and n % i == 0:
count = count + 1
while True:
n = n // i
if n % i != 0:
break
i = i + 2
return count
def main():
start = default_timer()
global primes
N = 150000
primes = sieve(N)
found = 0
i = 645
while not found and i < N - 3:
if primes[i] == 0 and primes[i+1] == 0 and primes[i+2] == 0 and primes[i+3] == 0:
if count_distinct_factors(i) == 4 and count_distinct_factors(i+1) == 4 and count_distinct_factors(i+2) == 4 and count_distinct_factors(i+3) == 4:
found = 1
i = i + 1
end = default_timer()
print('Project Euler, Problem 47')
print('Answer: {}'.format(i-1))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

22
Python/p048.py Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/python3
from timeit import default_timer
def main():
start = default_timer()
sum_ = 0
for i in range(1, 1001):
power = i ** i
sum_ = sum_ + power
end = default_timer()
print('Project Euler, Problem 48')
print('Answer: {}'.format(str(sum_)[-10:]))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

53
Python/p049.py Normal file
View File

@ -0,0 +1,53 @@
#!/usr/bin/python3
from numpy import zeros
from timeit import default_timer
from projecteuler import sieve
def check_digits(a, b):
digits1 = zeros(10, int)
digits2 = zeros(10, int)
while a > 0:
digits1[a%10] = digits1[a%10] + 1
a = a // 10
while b > 0:
digits2[b%10] = digits2[b%10] + 1
b = b // 10
for i in range(10):
if digits1[i] != digits2[i]:
return False
return True
def main():
start = default_timer()
N = 10000
primes = sieve(N)
found = 0
i = 1489
while i < N and found == 0:
if primes[i] == 1:
for j in range(1, 4255):
if i + 2 * j < N and primes[i+j] == 1 and primes[i+2*j] == 1 and\
check_digits(i, i+j) and check_digits(i, i+2*j):
found = 1
break
i = i + 1
end = default_timer()
print('Project Euler, Problem 49')
print('Answer: {}'.format(str(i-1)+str(i-1+j)+str(i-1+2*j)))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

41
Python/p050.py Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/python3
from timeit import default_timer
from projecteuler import sieve
def main():
start = default_timer()
N = 1000000
primes = sieve(N)
max_ = 0
max_p = 0
for i in range(2, N):
if primes[i] == 1:
count = 1
sum_ = i
j = i + 1
while j < N and sum_ < N:
if primes[j] == 1:
sum_ = sum_ + j
count = count + 1
if sum_ < N and primes[sum_] == 1 and count > max_:
max_ = count
max_p = sum_
j = j + 1
end = default_timer()
print('Project Euler, Problem 50')
print('Answer: {}'.format(max_p))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

1
Python/words.txt Normal file

File diff suppressed because one or more lines are too long