Add more solutions

Added solution for problems 102 and 124 in C, for problem 104
in python and for problem 145 in both C and python.
This commit is contained in:
2019-10-03 12:25:49 +02:00
parent 3836e41c75
commit a0c155d58c
9 changed files with 1393 additions and 2 deletions

57
Python/p104.py Normal file
View File

@ -0,0 +1,57 @@
# The Fibonacci sequence is defined by the recurrence relation:
#
# F_n = F_n1 + F_n2, where F_1 = 1 and F_2 = 1.
# It turns out that F_541, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 1-9 pandigital
# (contain all the digits 1 to 9, but not necessarily in order). And F_2749, which contains 575 digits, is the first Fibonacci number
# for which the first nine digits are 1-9 pandigital.
#
# Given that F_k is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k.
#!/usr/bin/python
from mpmath import matrix, mp
from timeit import default_timer
from projecteuler import is_pandigital
def main():
start = default_timer()
found = 0
fib1 = 1
fib2 = 1
i = 2
while not found:
# Calculate the next Fibonacci number modulo 10^9 and check if the result is 1-9 pandigital.
fibn = (fib1 + fib2) % 1000000000
fib1 = fib2
fib2 = fibn
i = i + 1
# If the last 9 digits of fib_n are pandigital, calculate the ith Fibonacci number using
# the matrix representation, with sufficient precision so that the at least the first
# 9 digits are correct (we don't need the whole number. If the first 9 digits are also
# pandigital, we found the solution.
if is_pandigital(fibn, 9):
fib_matrix = matrix(2)
fib_matrix[0, 0] = 1
fib_matrix[0, 1] = 1
fib_matrix[1, 0] = 1
fib_matrix[1, 1] = 0
fib = fib_matrix ** i
fib = int(fib[0, 1])
if is_pandigital(int(str(fib)[:9]), 9):
found = 1
end = default_timer()
print('Project Euler, Problem 104')
print('Answer: {}'.format(i))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()

38
Python/p145.py Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/python
# Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd (decimal) digits.
# For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers reversible; so 36, 63, 409, and 904 are reversible.
# Leading zeroes are not allowed in either n or reverse(n).
#
# There are 120 reversible numbers below one-thousand.
#
# How many reversible numbers are there below one-billion (109)?
from timeit import default_timer
def main():
start = default_timer()
N = 10000000
count = 0
# Brute force approach, sum each number and their reverse and
# check if there are only odd digits.
for i in range(11, N):
if i % 10 != 0:
s = str(i + int(''.join(reversed(str(i)))))
if not '0' in s and not '2' in s and not '4' in s and\
not '6' in s and not '8' in s:
count = count + 1
end = default_timer()
print('Project Euler, Problem 145')
print('Answer: {}'.format(count))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()