Use timing decorator for problems 71-80

This commit is contained in:
daniele 2023-06-07 20:39:55 +02:00
parent f54083389b
commit e8098ed5e5
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
10 changed files with 103 additions and 161 deletions

View File

@ -12,12 +12,12 @@
# of the fraction immediately to the left of 3/7. # of the fraction immediately to the left of 3/7.
from math import gcd from math import gcd
from timeit import default_timer
from projecteuler import timing
def main(): @timing
start = default_timer() def p071():
N = 1000000 N = 1000000
max_ = 0 max_ = 0
@ -43,13 +43,9 @@ def main():
max_n = n max_n = n
end = default_timer()
print('Project Euler, Problem 71') print('Project Euler, Problem 71')
print(f'Answer: {max_n}') print(f'Answer: {max_n}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p071()

View File

@ -10,14 +10,11 @@
# #
# How many elements would be contained in the set of reduced proper fractions for d ≤ 1,000,000? # How many elements would be contained in the set of reduced proper fractions for d ≤ 1,000,000?
from timeit import default_timer from projecteuler import sieve, phi, timing
from projecteuler import sieve, phi
def main(): @timing
start = default_timer() def p072():
N = 1000001 N = 1000001
count = 0 count = 0
@ -30,13 +27,9 @@ def main():
for i in range(2, N): for i in range(2, N):
count = count + phi(i, primes) count = count + phi(i, primes)
end = default_timer()
print('Project Euler, Problem 72') print('Project Euler, Problem 72')
print(f'Answer: {count}') print(f'Answer: {int(count)}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p072()

View File

@ -11,12 +11,12 @@
# How many fractions lie between 1/3 and 1/2 in the sorted set of reduced proper fractions for d ≤ 12,000? # How many fractions lie between 1/3 and 1/2 in the sorted set of reduced proper fractions for d ≤ 12,000?
from math import gcd from math import gcd
from timeit import default_timer
from projecteuler import timing
def main(): @timing
start = default_timer() def p073():
count = 0 count = 0
# For each denominator q, we need to find the fractions p/q for which # For each denominator q, we need to find the fractions p/q for which
@ -30,13 +30,9 @@ def main():
if gcd(j, i) == 1: if gcd(j, i) == 1:
count = count + 1 count = count + 1
end = default_timer()
print('Project Euler, Problem 73') print('Project Euler, Problem 73')
print(f'Answer: {count}') print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p073()

View File

@ -23,7 +23,8 @@
# How many chains, with a starting number below one million, contain exactly sixty non-repeating terms? # How many chains, with a starting number below one million, contain exactly sixty non-repeating terms?
from math import factorial from math import factorial
from timeit import default_timer
from projecteuler import timing
N = 1000000 N = 1000000
@ -44,21 +45,21 @@ def len_chain(n):
# Generate the next number of the chain by taking # Generate the next number of the chain by taking
# the digits of the current value, calculating the # the digits of the current value, calculating the
# factorials and adding them.*/ # factorials and adding them.
while value != 0: while value != 0:
tmp = tmp + factorial(value % 10) tmp = tmp + factorial(value % 10)
value = value // 10 value = value // 10
# If the chain length for the new value has already been # If the chain length for the new value has already been
# calculated before, use the saved value (only chains for # calculated before, use the saved value (only chains for
# values smaller than N are saved).*/ # values smaller than N are saved).
if tmp < N and chains[tmp] != 0: if tmp < N and chains[tmp] != 0:
return count + chains[tmp] return count + chains[tmp]
value = tmp value = tmp
# If the current value is already present in the chain, # If the current value is already present in the chain,
# the chain is finished.*/ # the chain is finished.
for i in range(count): for i in range(count):
if chain[i] == value: if chain[i] == value:
finished = 1 finished = 1
@ -70,9 +71,8 @@ def len_chain(n):
return count return count
def main(): @timing
start = default_timer() def p074():
count = 0 count = 0
# Simple brute force approach, for every number calculate # Simple brute force approach, for every number calculate
@ -81,13 +81,9 @@ def main():
if len_chain(i) == 60: if len_chain(i) == 60:
count = count + 1 count = count + 1
end = default_timer()
print('Project Euler, Problem 74') print('Project Euler, Problem 74')
print(f'Answer: {count}') print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p074()

View File

@ -18,12 +18,12 @@
# Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can exactly one integer sided right angle triangle be formed? # Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can exactly one integer sided right angle triangle be formed?
from math import gcd from math import gcd
from timeit import default_timer
from projecteuler import timing
def main(): @timing
start = default_timer() def p075():
N = 1500000 N = 1500000
l = [0] * (N+1) l = [0] * (N+1)
@ -68,13 +68,9 @@ def main():
if l[i] == 1: if l[i] == 1:
count = count + 1 count = count + 1
end = default_timer()
print('Project Euler, Problem 75') print('Project Euler, Problem 75')
print(f'Answer: {count}') print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p075()

View File

@ -11,14 +11,11 @@
# #
# How many different ways can one hundred be written as a sum of at least two positive integers?*/ # How many different ways can one hundred be written as a sum of at least two positive integers?*/
from timeit import default_timer from projecteuler import partition_fn, timing
from projecteuler import partition_fn
def main(): @timing
start = default_timer() def p076():
partitions = [0] * 101 partitions = [0] * 101
# The number of ways a number can be written as a sum is given by the partition function # The number of ways a number can be written as a sum is given by the partition function
@ -26,13 +23,9 @@ def main():
# The function is implemented in projecteuler.py. # The function is implemented in projecteuler.py.
n = partition_fn(100, partitions) - 1 n = partition_fn(100, partitions) - 1
end = default_timer()
print('Project Euler, Problem 76') print('Project Euler, Problem 76')
print(f'Answer: {n}') print(f'Answer: {n}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p076()

View File

@ -10,16 +10,13 @@
# #
# What is the first value which can be written as the sum of primes in over five thousand different ways? # What is the first value which can be written as the sum of primes in over five thousand different ways?
from timeit import default_timer from projecteuler import is_prime, timing
from projecteuler import is_prime
primes = [0] * 100 primes = [0] * 100
# Function using a simple recursive brute force approach # Function using a simple recursive brute force approach to find all the partitions.
# to find all the partitions.
def count(value, n, i, target): def count(value, n, i, target):
for j in range(i, 100): for j in range(i, 100):
value = value + primes[j] value = value + primes[j]
@ -36,9 +33,8 @@ def count(value, n, i, target):
return n return n
def main(): @timing
start = default_timer() def p077():
# Generate a list of the first 100 primes. # Generate a list of the first 100 primes.
i = 0 i = 0
j = 0 j = 0
@ -62,13 +58,9 @@ def main():
i = i + 1 i = i + 1
end = default_timer()
print('Project Euler, Problem 77') print('Project Euler, Problem 77')
print(f'Answer: {i}') print(f'Answer: {i}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p077()

View File

@ -13,32 +13,24 @@
# #
# Find the least value of n for which p(n) is divisible by one million. # Find the least value of n for which p(n) is divisible by one million.
from timeit import default_timer from projecteuler import partition_fn, timing
from projecteuler import partition_fn
def main(): @timing
start = default_timer() def p078():
N = 1000000 N = 1000000
partitions = [0] * N partitions = [0] * N
i = 0 i = 0
# Using the partition function to calculate the number of partitions, # Using the partition function to calculate the number of partitions, giving the result modulo N.
# giving the result modulo N.*/
while partition_fn(i, partitions, N) != 0: while partition_fn(i, partitions, N) != 0:
i = i + 1 i = i + 1
end = default_timer()
print('Project Euler, Problem 78') print('Project Euler, Problem 78')
print(f'Answer: {i}') print(f'Answer: {i}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p078()

View File

@ -10,12 +10,12 @@
import sys import sys
from itertools import permutations from itertools import permutations
from timeit import default_timer
from projecteuler import timing
def check_passcode(passcode, len_, logins, n): def check_passcode(passcode, len_, logins, n):
# For every login attempt, check if all the digits appear in the # For every login attempt, check if all the digits appear in the passcode in the correct order. Return 0 if a login attempt
# passcode in the correct order. Return 0 if a login attempt
# incompatible with the current passcode is found. # incompatible with the current passcode is found.
for i in range(n): for i in range(n):
k = 0 k = 0
@ -32,9 +32,8 @@ def check_passcode(passcode, len_, logins, n):
return 1 return 1
def main(): @timing
start = default_timer() def p079():
try: try:
with open('p079_keylog.txt', 'r', encoding='utf-8') as fp: with open('p079_keylog.txt', 'r', encoding='utf-8') as fp:
logins = fp.readlines() logins = fp.readlines()
@ -58,8 +57,7 @@ def main():
j = 0 j = 0
for i in range(10): for i in range(10):
# To generate the passcode, only use the digits present in the # To generate the passcode, only use the digits present in the login attempts.
# login attempts.
if digits[i] > 0: if digits[i] > 0:
passcode_digits[j] = i passcode_digits[j] = i
j = j + 1 j = j + 1
@ -70,8 +68,7 @@ def main():
while not found: while not found:
passcode = [0] * len_ passcode = [0] * len_
# For the current length, generate the first passcode with the # For the current length, generate the first passcode with the digits in order.
# digits in order.
for i in range(len_): for i in range(len_):
passcode[i] = passcode_digits[i] passcode[i] = passcode_digits[i]
@ -93,13 +90,9 @@ def main():
# If the passcode has not yet been found, try a longer passcode. # If the passcode has not yet been found, try a longer passcode.
len_ = len_ + 1 len_ = len_ + 1
end = default_timer()
print('Project Euler, Problem 79') print('Project Euler, Problem 79')
print(f'Answer: {res}') print(f'Answer: {res}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p079()

View File

@ -8,9 +8,10 @@
# For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits # For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits
# for all the irrational square roots # for all the irrational square roots
from timeit import default_timer
from mpmath import sqrt, mp from mpmath import sqrt, mp
from projecteuler import timing
def is_square(n): def is_square(n):
p = sqrt(n) p = sqrt(n)
@ -19,9 +20,8 @@ def is_square(n):
return bool(p == m) return bool(p == m)
def main(): @timing
start = default_timer() def p080():
# Set the precision to 100 digits # Set the precision to 100 digits
mp.dps = 102 mp.dps = 102
@ -29,8 +29,7 @@ def main():
for i in range(2, 100): for i in range(2, 100):
if not is_square(i): if not is_square(i):
# Calculate the square root of the current number with the given precision # Calculate the square root of the current number with the given precision and sum the digits to the total.
# and sum the digits to the total.
root = str(sqrt(i)) root = str(sqrt(i))
sum_ = sum_ + int(root[0]) sum_ = sum_ + int(root[0])
@ -38,13 +37,9 @@ def main():
for j in range(2, 101): for j in range(2, 101):
sum_ = sum_ + int(root[j]) sum_ = sum_ + int(root[j])
end = default_timer()
print('Project Euler, Problem 80') print('Project Euler, Problem 80')
print(f'Answer: {sum_}') print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p080()