From 8b44d6a68e5d218c4d438aca6373ff33637ff60b Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Wed, 7 Jun 2023 21:09:16 +0200 Subject: [PATCH] Use timing decorator for last solved problems --- Python/p081.py | 14 +++++--------- Python/p082.py | 21 +++++++-------------- Python/p083.py | 14 ++++---------- Python/p088.py | 2 +- Python/p104.py | 24 +++++++++--------------- Python/p145.py | 17 ++++++----------- Python/p206.py | 21 ++++++++------------- 7 files changed, 40 insertions(+), 73 deletions(-) diff --git a/Python/p081.py b/Python/p081.py index dd327a5..da0289c 100644 --- a/Python/p081.py +++ b/Python/p081.py @@ -13,7 +13,8 @@ # by only moving right and down. import sys -from timeit import default_timer + +from projecteuler import timing def sum_paths(matrix, m, n): @@ -31,9 +32,8 @@ def sum_paths(matrix, m, n): return matrix[0][0] -def main(): - start = default_timer() - +@timing +def p081(): try: with open('matrix.txt', 'r', encoding='utf-8') as fp: matrix = fp.readlines() @@ -49,13 +49,9 @@ def main(): dist = sum_paths(matrix, 80, 80) - end = default_timer() - print('Project Euler, Problem 81') print(f'Answer: {dist}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p081() diff --git a/Python/p082.py b/Python/p082.py index 1358e64..560641a 100644 --- a/Python/p082.py +++ b/Python/p082.py @@ -16,16 +16,14 @@ # Find the minimal path sum, in matrix.txt, a 31K text file containing a 80 by 80 matrix, from the left column to the right column. import sys -from timeit import default_timer from numpy import zeros -from projecteuler import dijkstra +from projecteuler import dijkstra, timing -def main(): - start = default_timer() - +@timing +def p082(): try: with open('matrix.txt', 'r', encoding='utf-8') as fp: matrix = fp.readlines() @@ -43,24 +41,19 @@ def main(): min_path = 999999999 -# Use Dijkstra's algorithm starting from all possible nodes -# in the first column. + # Use Dijkstra's algorithm starting from all possible nodes in the first column. for i in range(80): dijkstra(matrix, distances, 80, 80, 1, 0, i) -# For the current starting node, check if there is an ending node -# with a smaller path cost than the current minimum. + # For the current starting node, check if there is an ending node + # with a smaller path cost than the current minimum. for j in range(80): if distances[j][79] < min_path: min_path = distances[j][79] - end = default_timer() - print('Project Euler, Problem 82') print(f'Answer: {min_path}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p082() diff --git a/Python/p083.py b/Python/p083.py index 007af42..af4cbda 100644 --- a/Python/p083.py +++ b/Python/p083.py @@ -15,16 +15,14 @@ # from the top left to the bottom right by moving left, right, up, and down. import sys -from timeit import default_timer from numpy import zeros -from projecteuler import dijkstra +from projecteuler import dijkstra, timing -def main(): - start = default_timer() - +@timing +def p083(): try: with open('matrix.txt', 'r', encoding='utf-8') as fp: matrix = fp.readlines() @@ -43,13 +41,9 @@ def main(): dist = distances[79][79] - end = default_timer() - print('Project Euler, Problem 83') print(f'Answer: {dist}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p083() diff --git a/Python/p088.py b/Python/p088.py index 3fffe72..b43159c 100644 --- a/Python/p088.py +++ b/Python/p088.py @@ -18,7 +18,7 @@ # # What is the sum of all the minimal product-sum numbers for 2<=k<=12000? -from math import floor, sqrt, prod +from math import prod from projecteuler import sieve, timing diff --git a/Python/p104.py b/Python/p104.py index 53bb88c..42e15ca 100644 --- a/Python/p104.py +++ b/Python/p104.py @@ -10,35 +10,33 @@ # 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. import sys -from timeit import default_timer from mpmath import matrix -from projecteuler import is_pandigital +from projecteuler import is_pandigital, timing sys.set_int_max_str_digits(70000) -def main(): - start = default_timer() - +@timing +def p104(): 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. + # 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 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 @@ -52,13 +50,9 @@ def main(): if is_pandigital(int(str(fib)[:9]), 9): found = 1 - end = default_timer() - print('Project Euler, Problem 104') print(f'Answer: {i}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p104() diff --git a/Python/p145.py b/Python/p145.py index 28c3c22..a990e8f 100644 --- a/Python/p145.py +++ b/Python/p145.py @@ -8,18 +8,17 @@ # # How many reversible numbers are there below one-billion (109)? -from timeit import default_timer +from projecteuler import timing -def main(): - start = default_timer() - +@timing +def p145(): N = 1000000000 count = 0 -# Brute force approach, sum each number and their reverse and -# check if there are only odd digits. + # 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))))) @@ -28,13 +27,9 @@ def main(): '6' not in s and '8' not in s: count = count + 1 - end = default_timer() - print('Project Euler, Problem 145') print(f'Answer: {count}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p145() diff --git a/Python/p206.py b/Python/p206.py index cec314d..bd247fa 100644 --- a/Python/p206.py +++ b/Python/p206.py @@ -3,26 +3,25 @@ # Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, # where each “_” is a single digit. -from timeit import default_timer +from projecteuler import timing -def main(): - start = default_timer() - -# Since the square of n has 19 digits, n must be at least 10^9. +@timing +def p206(): + # Since the square of n has 19 digits, n must be at least 10^9. n = 1000000000 found = 0 while not found: -# If the square on n ends with 10, n must be divisible by 10. + # If the square on n ends with 10, n must be divisible by 10. n = n + 10 p = n * n -# A square divisible by 10 is also divisible by 100. + # A square divisible by 10 is also divisible by 100. if p % 100 != 0: continue -# Check if the digits of the square correspond to the given pattern. + # Check if the digits of the square correspond to the given pattern. i = 9 p = p // 100 @@ -35,13 +34,9 @@ def main(): if p == 0: found = 1 - end = default_timer() - print('Project Euler, Problem 206') print(f'Answer: {n}') - print(f'Elapsed time: {end - start:.9f} seconds') - if __name__ == '__main__': - main() + p206()