From 885084211ae93958054142e9aaa867152d1223c5 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Wed, 7 Jun 2023 17:46:10 +0200 Subject: [PATCH] Use timing decorator for first 10 problems --- Python/p001.py | 17 ++++++----------- Python/p002.py | 18 ++++++------------ Python/p003.py | 24 ++++++++---------------- Python/p004.py | 22 ++++++++-------------- Python/p005.py | 16 +++++----------- Python/p006.py | 15 +++++---------- Python/p007.py | 22 ++++++++-------------- Python/p008.py | 17 ++++++----------- Python/p009.py | 17 ++++++----------- Python/p010.py | 20 +++++++------------- 10 files changed, 65 insertions(+), 123 deletions(-) diff --git a/Python/p001.py b/Python/p001.py index c195a2b..ec5b534 100644 --- a/Python/p001.py +++ b/Python/p001.py @@ -4,27 +4,22 @@ # # Find the sum of all the multiples of 3 or 5 below 1000. -from timeit import default_timer +from projecteuler import timing -def main(): - start = default_timer() - +@timing +def p001(): sum_ = 0 -# Simple brute-force approach: try every number between 3 and 999, -# check if it's a multiple of 3 or 5, if yes add it to the total. + # Simple brute-force approach: try every number between 3 and 999, + # check if it's a multiple of 3 or 5, if yes add it to the total. for i in range(3, 1000): if i % 3 == 0 or i % 5 == 0: sum_ += i - end = default_timer() - print('Project Euler, Problem 1') print(f'Answer: {sum_}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p001() diff --git a/Python/p002.py b/Python/p002.py index 63ed494..03d0ba1 100644 --- a/Python/p002.py +++ b/Python/p002.py @@ -6,13 +6,11 @@ # # By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. -from timeit import default_timer +from projecteuler import timing -def main(): - - start = default_timer() - +@timing +def p002(): N = 4000000 fib1 = 1 @@ -20,8 +18,8 @@ def main(): fibn = fib1 + fib2 sum_ = 2 -# Simple brute-force approach: generate every value in the Fibonacci -# sequence smaller than 4 million and if it's even add it to the total. + # Simple brute-force approach: generate every value in the Fibonacci + # sequence smaller than 4 million and if it's even add it to the total. while fibn < N: if fibn % 2 == 0: sum_ = sum_ + fibn @@ -30,13 +28,9 @@ def main(): fib2 = fibn fibn = fib1 + fib2 - end = default_timer() - print('Project Euler, Problem 2') print(f'Answer: {sum_}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p002() diff --git a/Python/p003.py b/Python/p003.py index ed283a1..3eaa203 100644 --- a/Python/p003.py +++ b/Python/p003.py @@ -4,27 +4,24 @@ # # What is the largest prime factor of the number 600851475143? -from timeit import default_timer - -from projecteuler import is_prime +from projecteuler import is_prime, timing # Recursive approach: if num is prime, return num, otherwise # recursively look for the largest prime factor of num divided # by its prime factors until only the largest remains. def max_prime_factor(num): -# Use function defined in projecteuler.py to check if a number is prime. + # Use function defined in projecteuler.py to check if a number is prime. if is_prime(num): return num -# If num is even, find the largest prime factor of num/2. + # If num is even, find the largest prime factor of num/2. if num % 2 == 0: return max_prime_factor(num // 2) i = 3 -# If num is divisible by i and i is prime, find largest -# prime factor of num/i. + # If num is divisible by i and i is prime, find largest prime factor of num/i. while True: if num % i == 0: if is_prime(i): @@ -32,22 +29,17 @@ def max_prime_factor(num): i = i + 2 -# Should never get here + # Should never get here return -1 -def main(): - start = default_timer() - +@timing +def p003(): res = max_prime_factor(600851475143) - end = default_timer() - print('Project Euler, Problem 3') print(f'Answer: {res}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p003() diff --git a/Python/p004.py b/Python/p004.py index e6737ca..a7f776b 100644 --- a/Python/p004.py +++ b/Python/p004.py @@ -4,32 +4,26 @@ # # Find the largest palindrome made from the product of two 3-digit numbers. -from timeit import default_timer -from projecteuler import is_palindrome +from projecteuler import is_palindrome, timing -def main(): - start = default_timer() - +@timing +def p004(): max_ = 0 -# Using a brute-force approach: generate every product of 3-digit numbers -# and check if it's palindrome. If the product found is greater than the -# current maximum, save the current product. + # Using a brute-force approach: generate every product of 3-digit numbers + # and check if it's palindrome. If the product found is greater than the + # current maximum, save the current product. for i in range(999, 99, -1): for j in range(i, 99, -1): num = i * j -# Use the function defined in projecteuler.py to check if a number is palindrome. + # Use the function defined in projecteuler.py to check if a number is palindrome. if num > max_ and is_palindrome(num, 10): max_ = num - end = default_timer() - print('Project Euler, Problem 4') print(f'Answer: {max_}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p004() diff --git a/Python/p005.py b/Python/p005.py index e49e28c..74cf7db 100644 --- a/Python/p005.py +++ b/Python/p005.py @@ -4,26 +4,20 @@ # # What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? -from timeit import default_timer -from projecteuler import lcmm +from projecteuler import lcmm, timing -def main(): - start = default_timer() - +@timing +def p005(): values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) -# Function define in projecteuler.py to find the least common multiple of multiple numbers. + # Function define in projecteuler.py to find the least common multiple of multiple numbers. res = lcmm(values, 20) - end = default_timer() - print('Project Euler, Problem 5') print(f'Answer: {res}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p005() diff --git a/Python/p006.py b/Python/p006.py index 08ece15..09ad078 100644 --- a/Python/p006.py +++ b/Python/p006.py @@ -12,29 +12,24 @@ # # Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. -from timeit import default_timer +from projecteuler import timing -def main(): - start = default_timer() - +@timing +def p006(): sum_squares = 0 square_sum = 0 -# Straightforward brute-force approach. + # Straightforward brute-force approach. for i in range(1, 101): sum_squares = sum_squares + i * i square_sum = square_sum + i square_sum = square_sum * square_sum - end = default_timer() - print('Project Euler, Problem 6') print(f'Answer: {square_sum - sum_squares}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p006() diff --git a/Python/p007.py b/Python/p007.py index fd61e42..fbfc2a1 100644 --- a/Python/p007.py +++ b/Python/p007.py @@ -4,32 +4,26 @@ # # What is the 10 001st prime number? -from timeit import default_timer -from projecteuler import is_prime +from projecteuler import is_prime, timing -def main(): - start = default_timer() - +@timing +def p007(): count = 1 n = 1 -# Brute force approach: start with count=1 and check every odd number -# (2 is the only even prime), if it's prime increment count, until the -# target prime is reached. + # Brute force approach: start with count=1 and check every odd number + # (2 is the only even prime), if it's prime increment count, until the + # target prime is reached. while count != 10001: n = n + 2 -# Use the function in projecteuler.py to check if a number is prime. + # Use the function in projecteuler.py to check if a number is prime. if is_prime(n): count = count + 1 - end = default_timer() - print('Project Euler, Problem 7') print(f'Answer: {n}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p007() diff --git a/Python/p008.py b/Python/p008.py index 9531e73..261612b 100644 --- a/Python/p008.py +++ b/Python/p008.py @@ -25,12 +25,11 @@ # # Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? -from timeit import default_timer +from projecteuler import timing -def main(): - start = default_timer() - +@timing +def p008(): number = '73167176531330624919225119674426574742355349194934' +\ '96983520312774506326239578318016984801869478851843' +\ '85861560789112949495459501737958331952853208805511' +\ @@ -51,12 +50,12 @@ def main(): '05886116467109405077541002256983155200055935729725' +\ '71636269561882670428252483600823257530420752963450' -# Transform the string into a list of integers + # Transform the string into a list of integers number = list(map(int, number)) max_ = 0 -# Calculate all the 13-digit products, and save the maximum + # Calculate all the 13-digit products, and save the maximum for i in range(1000-13): curr = number[i:i+13] prod = 1 @@ -67,13 +66,9 @@ def main(): if prod > max_: max_ = prod - end = default_timer() - print('Project Euler, Problem 8') print(f'Answer: {max_}') - print(f'Elapsed time: {end - start:.9f} seconds'.format(end - start)) - if __name__ == '__main__': - main() + p008() diff --git a/Python/p009.py b/Python/p009.py index 18bda2c..616b706 100644 --- a/Python/p009.py +++ b/Python/p009.py @@ -11,18 +11,17 @@ # Find the product abc. from math import gcd -from timeit import default_timer +from projecteuler import timing -def main(): - start = default_timer() - +@timing +def p009(): found = 0 m = 2 -# Brute force approach: generate all the Pythagorean triplets using -# Euclid's formula, until the one where a+b+c=1000 is found. + # Brute force approach: generate all the Pythagorean triplets using + # Euclid's formula, until the one where a+b+c=1000 is found. while not found: for n in range(1, m): if found == 1: @@ -58,13 +57,9 @@ def main(): m = m + 1 - end = default_timer() - print('Project Euler, Problem 9') print(f'Answer: {a * b * c}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p009() diff --git a/Python/p010.py b/Python/p010.py index 64d791b..16c62be 100644 --- a/Python/p010.py +++ b/Python/p010.py @@ -4,32 +4,26 @@ # # Find the sum of all the primes below two million. -from timeit import default_timer -from projecteuler import sieve +from projecteuler import sieve, timing -def main(): - start = default_timer() - +@timing +def p010(): N = 2000000 -# Use the function in projecteuler.py implementing the -# Sieve of Eratosthenes algorithm to generate primes. + # Use the function in projecteuler.py implementing the + # Sieve of Eratosthenes algorithm to generate primes. primes = sieve(N) sum_ = 0 -# Sum all the primes + # Sum all the primes for i in range(N): if primes[i] == 1: sum_ = sum_ + i - end = default_timer() - print('Project Euler, Problem 10') print(f'Answer: {sum_}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p010()