From e8098ed5e55f0b367c7d5076ad67adf84287fa55 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Wed, 7 Jun 2023 20:39:55 +0200 Subject: [PATCH] Use timing decorator for problems 71-80 --- Python/p071.py | 28 ++++++++++++---------------- Python/p072.py | 25 +++++++++---------------- Python/p073.py | 22 +++++++++------------- Python/p074.py | 34 +++++++++++++++------------------- Python/p075.py | 36 ++++++++++++++++-------------------- Python/p076.py | 21 +++++++-------------- Python/p077.py | 26 +++++++++----------------- Python/p078.py | 18 +++++------------- Python/p079.py | 35 ++++++++++++++--------------------- Python/p080.py | 19 +++++++------------ 10 files changed, 103 insertions(+), 161 deletions(-) diff --git a/Python/p071.py b/Python/p071.py index 63741d3..ec205b9 100644 --- a/Python/p071.py +++ b/Python/p071.py @@ -12,22 +12,22 @@ # of the fraction immediately to the left of 3/7. from math import gcd -from timeit import default_timer + +from projecteuler import timing -def main(): - start = default_timer() - +@timing +def p071(): N = 1000000 max_ = 0 -# For each denominator q, we need to find the biggest numerator p for which -# p/q 1: n /= gcd(i, j) d /= gcd(i, j) max_n = n - end = default_timer() - print('Project Euler, Problem 71') print(f'Answer: {max_n}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p071() diff --git a/Python/p072.py b/Python/p072.py index 23646e4..783ed84 100644 --- a/Python/p072.py +++ b/Python/p072.py @@ -10,33 +10,26 @@ # # 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 +from projecteuler import sieve, phi, timing -def main(): - start = default_timer() - +@timing +def p072(): N = 1000001 count = 0 primes = sieve(N) -# For any denominator d, the number of reduced proper fractions is -# the number of fractions n/d where gcd(n, d)=1, which is the definition -# of Euler's Totient Function phi. It's sufficient to calculate phi for each -# denominator and sum the value. + # For any denominator d, the number of reduced proper fractions is + # the number of fractions n/d where gcd(n, d)=1, which is the definition + # of Euler's Totient Function phi. It's sufficient to calculate phi for each + # denominator and sum the value. for i in range(2, N): count = count + phi(i, primes) - end = default_timer() - print('Project Euler, Problem 72') - print(f'Answer: {count}') - - print(f'Elapsed time: {end - start:.9f} seconds') + print(f'Answer: {int(count)}') if __name__ == '__main__': - main() + p072() diff --git a/Python/p073.py b/Python/p073.py index cd0b9cd..f5ae550 100644 --- a/Python/p073.py +++ b/Python/p073.py @@ -11,32 +11,28 @@ # 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 timeit import default_timer + +from projecteuler import timing -def main(): - start = default_timer() - +@timing +def p073(): count = 0 -# For each denominator q, we need to find the fractions p/q for which -# 1/3

=2 and n=2 and n= 2 until the one that can be written in over -# 5000 ways is found. + # Use a function to count the number of prime partitions for + # each number >= 2 until the one that can be written in over + # 5000 ways is found. while True: n = count(0, 0, 0, i) @@ -62,13 +58,9 @@ def main(): i = i + 1 - end = default_timer() - print('Project Euler, Problem 77') print(f'Answer: {i}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p077() diff --git a/Python/p078.py b/Python/p078.py index 66ee8d5..cbbc9e8 100644 --- a/Python/p078.py +++ b/Python/p078.py @@ -13,32 +13,24 @@ # # 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 +from projecteuler import partition_fn, timing -def main(): - start = default_timer() - +@timing +def p078(): N = 1000000 partitions = [0] * N i = 0 -# Using the partition function to calculate the number of partitions, -# giving the result modulo N.*/ + # Using the partition function to calculate the number of partitions, giving the result modulo N. while partition_fn(i, partitions, N) != 0: i = i + 1 - end = default_timer() - print('Project Euler, Problem 78') print(f'Answer: {i}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p078() diff --git a/Python/p079.py b/Python/p079.py index 7f8b02d..c3a1bab 100644 --- a/Python/p079.py +++ b/Python/p079.py @@ -10,13 +10,13 @@ import sys from itertools import permutations -from timeit import default_timer + +from projecteuler import timing def check_passcode(passcode, len_, logins, n): -# For every login attempt, check if all the digits appear in the -# passcode in the correct order. Return 0 if a login attempt -# incompatible with the current passcode is found. + # For every login attempt, check if all the digits appear in the passcode in the correct order. Return 0 if a login attempt + # incompatible with the current passcode is found. for i in range(n): k = 0 for j in range(len_): @@ -32,9 +32,8 @@ def check_passcode(passcode, len_, logins, n): return 1 -def main(): - start = default_timer() - +@timing +def p079(): try: with open('p079_keylog.txt', 'r', encoding='utf-8') as fp: logins = fp.readlines() @@ -48,7 +47,7 @@ def main(): for i in logins: keylog = int(i) -# Check which digits are present in the login attempts. + # Check which digits are present in the login attempts. while True: digits[keylog % 10] = digits[keylog % 10] + 1 keylog = keylog // 10 @@ -58,8 +57,7 @@ def main(): j = 0 for i in range(10): -# To generate the passcode, only use the digits present in the -# login attempts. + # To generate the passcode, only use the digits present in the login attempts. if digits[i] > 0: passcode_digits[j] = i j = j + 1 @@ -70,18 +68,17 @@ def main(): while not found: passcode = [0] * len_ -# For the current length, generate the first passcode with the -# digits in order. + # For the current length, generate the first passcode with the digits in order. for i in range(len_): passcode[i] = passcode_digits[i] -# Check if the passcode is compatible with the login attempts. + # Check if the passcode is compatible with the login attempts. if check_passcode(passcode, len_, logins, 50): found = 1 break -# For the given length, check every permutation until the correct -# passcode has been found, or all the permutations have been tried. + # For the given length, check every permutation until the correct + # passcode has been found, or all the permutations have been tried. passcodes = permutations(passcode, len_) for i in passcodes: @@ -90,16 +87,12 @@ def main(): res = ''.join(map(str, i)) break -# 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 - end = default_timer() - print('Project Euler, Problem 79') print(f'Answer: {res}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p079() diff --git a/Python/p080.py b/Python/p080.py index 382d4d9..80268df 100644 --- a/Python/p080.py +++ b/Python/p080.py @@ -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 all the irrational square roots -from timeit import default_timer from mpmath import sqrt, mp +from projecteuler import timing + def is_square(n): p = sqrt(n) @@ -19,18 +20,16 @@ def is_square(n): return bool(p == m) -def main(): - start = default_timer() - -# Set the precision to 100 digits +@timing +def p080(): + # Set the precision to 100 digits mp.dps = 102 sum_ = 0 for i in range(2, 100): if not is_square(i): -# Calculate the square root of the current number with the given precision -# and sum the digits to the total. + # Calculate the square root of the current number with the given precision and sum the digits to the total. root = str(sqrt(i)) sum_ = sum_ + int(root[0]) @@ -38,13 +37,9 @@ def main(): for j in range(2, 101): sum_ = sum_ + int(root[j]) - end = default_timer() - print('Project Euler, Problem 80') print(f'Answer: {sum_}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p080()