Use timing decorator for problems 21-40

This commit is contained in:
daniele 2023-06-07 18:17:42 +02:00
parent 634eb3f750
commit be5e97dfbb
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
20 changed files with 172 additions and 288 deletions

View File

@ -9,20 +9,18 @@
# Evaluate the sum of all the amicable numbers under 10000.
from timeit import default_timer
from projecteuler import sum_of_divisors
from projecteuler import sum_of_divisors, timing
def main():
start = default_timer()
@timing
def p021():
sum_ = 0
for i in range(2, 10000):
# Calculate the sum of proper divisors with the function
# implemented in projecteuler.py.
n = sum_of_divisors(i)
# If i!=n and the sum of proper divisors of n=i,
# sum the pair of numbers and add it to the total.
if i != n and sum_of_divisors(n) == i:
@ -30,13 +28,9 @@ def main():
sum_ = sum_ // 2
end = default_timer()
print('Project Euler, Problem 21')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p021()

View File

@ -9,12 +9,12 @@
# What is the total of all the name scores in the file?
import sys
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p022():
try:
with open('p022_names.txt', 'r', encoding='utf-8') as fp:
names = list(fp.readline().replace('"', '').split(','))
@ -37,13 +37,9 @@ def main():
sum_ = sum_ + score
i = i + 1
end = default_timer()
print('Project Euler, Problem 22')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p022()

View File

@ -12,18 +12,15 @@
#
# Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
from timeit import default_timer
from projecteuler import sum_of_divisors
from projecteuler import sum_of_divisors, timing
def is_abundant(n):
return sum_of_divisors(n) > n
def main():
start = default_timer()
@timing
def p023():
ab_nums = [False] * 28124
# Find all abundant numbers smaller than 28123.
@ -50,13 +47,9 @@ def main():
if not sums[i]:
sum_ = sum_ + i
end = default_timer()
print('Project Euler, Problem 23')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p023()

View File

@ -8,23 +8,19 @@
# What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
from itertools import permutations
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p024():
# Generate all the permutations in lexicographic order and get the millionth one.
perm = list(permutations('0123456789'))
res = int(''.join(map(str, perm[999999])))
end = default_timer()
print('Project Euler, Problem 24')
print(f'Answer: {res}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p024()

View File

@ -21,12 +21,11 @@
#
# What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p025():
fib1 = 1
fib2 = 1
fibn = fib1 + fib2
@ -40,13 +39,9 @@ def main():
fibn = fib1 + fib2
i = i + 1
end = default_timer()
print('Project Euler, Problem 25')
print(f'Answer: {i}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p025()

View File

@ -16,27 +16,24 @@
#
# Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p026():
max_ = 0
for i in range(2, 1000):
j = i
# The repeating cycle of 1/(2^a*5^b*p^c*...) is equal to
# that of 1/p^c*..., so factors 2 and 5 can be eliminated.
# The repeating cycle of 1/(2^a*5^b*p^c*...) is equal to that of 1/p^c*..., so factors 2 and 5 can be eliminated.
while j % 2 == 0 and j > 1:
j = j // 2
while j % 5 == 0 and j > 1:
j = j // 5
# If the denominator had only factors 2 and 5, there is no
# repeating cycle.
# If the denominator had only factors 2 and 5, there is no repeating cycle.
if j == 1:
n = 0
else:
@ -57,13 +54,9 @@ def main():
max_ = n
max_n = i
end = default_timer()
print('Project Euler, Problem 26')
print(f'Answer: {max_n}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p026()

View File

@ -19,14 +19,11 @@
#
# Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
from timeit import default_timer
from projecteuler import is_prime
from projecteuler import is_prime, timing
def main():
start = default_timer()
@timing
def p027():
max_ = 0
# Brute force approach, optimized by checking only values of b where b is prime.
@ -51,13 +48,9 @@ def main():
save_a = a
save_b = b
end = default_timer()
print('Project Euler, Problem 27')
print(f'Answer: {save_a * save_b}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p027()

View File

@ -12,12 +12,11 @@
#
# What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p028():
N = 1001
limit = N * N
@ -38,13 +37,9 @@ def main():
sum_ = sum_ + j
i = (i + 1) % 4
end = default_timer()
print('Project Euler, Problem 28')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p028()

View File

@ -13,14 +13,13 @@
#
# How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?
from timeit import default_timer
from numpy import zeros
from projecteuler import timing
def main():
start = default_timer()
@timing
def p029():
powers = zeros(9801)
# Generate all the powers
@ -39,13 +38,9 @@ def main():
if powers[i] != powers[i-1]:
count = count + 1
end = default_timer()
print('Project Euler, Problem 29')
print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p029()

View File

@ -12,12 +12,11 @@
#
# Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p030():
tot = 0
# Straightforward brute force approach. The limit is chosen considering that
@ -35,13 +34,9 @@ def main():
if sum_ == i:
tot = tot + i
end = default_timer()
print('Project Euler, Problem 30')
print(f'Answer: {tot}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p030()

View File

@ -10,7 +10,7 @@
#
# How many different ways can £2 be made using any number of coins?
from timeit import default_timer
from projecteuler import timing
# Simple recursive function that tries every combination.
@ -29,20 +29,15 @@ def count(coins, value, n, i):
return n
def main():
start = default_timer()
@timing
def p031():
coins = [1, 2, 5, 10, 20, 50, 100, 200]
n = count(coins, 0, 0, 0)
end = default_timer()
print('Project Euler, Problem 31')
print(f'Answer: {n}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p031()

View File

@ -9,17 +9,14 @@
#
# HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
from timeit import default_timer
import numpy as np
from numpy import zeros
from projecteuler import is_pandigital
from projecteuler import is_pandigital, timing
def main():
start = default_timer()
@timing
def p032():
n = 0
# Initially I used a bigger array, but printing the resulting products
# shows that 10 values are sufficient.
@ -79,13 +76,9 @@ def main():
if products[i] != products[i-1]:
sum_ = sum_ + products[i]
end = default_timer()
print('Project Euler, Problem 32')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p032()

View File

@ -11,12 +11,12 @@
# If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
from math import gcd
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p033():
prod_n = 1
prod_d = 1
@ -39,13 +39,9 @@ def main():
# Find the greater common divisor of the fraction found.
div = gcd(prod_n, prod_d)
end = default_timer()
print('Project Euler, Problem 33')
print(f'Answer: {prod_d // div}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p033()

View File

@ -7,14 +7,14 @@
# Note: as 1! = 1 and 2! = 2 are not sums they are not included.
from math import factorial
from timeit import default_timer
from numpy import ones
from projecteuler import timing
def main():
start = default_timer()
@timing
def p034():
a = 10
sum_ = 0
factorials = ones(10, int)
@ -38,13 +38,9 @@ def main():
a = a + 1
end = default_timer()
print('Project Euler, Problem 34')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p034()

View File

@ -6,9 +6,7 @@
#
# How many circular primes are there below one million?
from timeit import default_timer
from projecteuler import sieve
from projecteuler import sieve, timing
# Calculate all primes below one million, then check if they're circular.
@ -47,9 +45,8 @@ def is_circular_prime(n):
return True
def main():
start = default_timer()
@timing
def p035():
count = 13
# Calculate all primes below one million, then check if they're circular.
@ -57,13 +54,9 @@ def main():
if is_circular_prime(i):
count = count + 1
end = default_timer()
print('Project Euler, Problem 35')
print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p035()

View File

@ -6,14 +6,11 @@
#
# (Please note that the palindromic number, in either base, may not include leading zeros.)
from timeit import default_timer
from projecteuler import is_palindrome
from projecteuler import is_palindrome, timing
def main():
start = default_timer()
@timing
def p036():
N = 1000000
sum_ = 0
@ -25,13 +22,9 @@ def main():
if is_palindrome(i, 10) and is_palindrome(i, 2):
sum_ = sum_ + i
end = default_timer()
print('Project Euler, Problem 36')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p036()

View File

@ -7,9 +7,7 @@
#
# NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
from timeit import default_timer
from projecteuler import is_prime
from projecteuler import is_prime, timing
def is_tr_prime(n):
@ -27,8 +25,7 @@ def is_tr_prime(n):
return False
tmp = tmp // 10
# Starting from the last digit, check if it's prime, then
# add back one digit at a time on the left and check if it
# Starting from the last digit, check if it's prime, then add back one digit at a time on the left and check if it
# is prime. Return 0 when it isn't.
i = 10
tmp = n % i
@ -43,9 +40,8 @@ def is_tr_prime(n):
return True
def main():
start = default_timer()
@timing
def p037():
i = 0
n = 1
sum_ = 0
@ -57,13 +53,9 @@ def main():
i = i + 1
n = n + 1
end = default_timer()
print('Project Euler, Problem 37')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p037()

View File

@ -13,14 +13,11 @@
#
# What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1?
from timeit import default_timer
from projecteuler import is_pandigital
from projecteuler import is_pandigital, timing
def main():
start = default_timer()
@timing
def p038():
max_ = 0
# A brute force approach is used, starting with 1 and multiplying
@ -54,13 +51,9 @@ def main():
if n > 987654321:
break
end = default_timer()
print('Project Euler, Problem 38')
print(f'Answer: {max_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p038()

View File

@ -6,14 +6,13 @@
#
# For which value of p ≤ 1000, is the number of solutions maximised?
from timeit import default_timer
from numpy import zeros
from projecteuler import timing
def main():
start = default_timer()
@timing
def p039():
max_ = 0
savedc = zeros(1000, int)
@ -32,8 +31,7 @@ def main():
b = 2 * m * n
c = m * m + n * n
# Increase counter if a+b+c=p and the triplet is new,
# then save the value of c to avoid counting the same
# Increase counter if a+b+c=p and the triplet is new, then save the value of c to avoid counting the same
# triplet more than once.
if a + b + c == p and savedc[c] == 0:
savedc[c] = 1
@ -44,8 +42,7 @@ def main():
tmpb = b
tmpc = c
# Check all the triplets obtained multiplying a, b and c
# for integer numbers, until the perimeters exceeds p.
# Check all the triplets obtained multiplying a, b and c for integer numbers, until the perimeters exceeds p.
while tmpa + tmpb + tmpc < p:
tmpa = a * i
tmpb = b * i
@ -66,13 +63,9 @@ def main():
max_ = count
res = p
end = default_timer()
print('Project Euler, Problem 39')
print(f'Answer: {res}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p039()

View File

@ -10,14 +10,13 @@
#
# d_1 × d_10 × d_100 × d_1000 × d_10000 × d_100000 × d_1000000
from timeit import default_timer
from numpy import zeros
from projecteuler import timing
def main():
start = default_timer()
@timing
def p040():
digits = zeros(1000005, int)
i = 1
value = 1
@ -64,13 +63,9 @@ def main():
# Calculate the product.
n = digits[0] * digits[9] * digits[99] * digits[999] * digits[9999] * digits[99999] * digits[999999]
end = default_timer()
print('Project Euler, Problem 40')
print(f'Answer: {n}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p040()