Use timing decorator for last solved problems

This commit is contained in:
daniele 2023-06-07 21:09:16 +02:00
parent e8098ed5e5
commit 8b44d6a68e
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
7 changed files with 40 additions and 73 deletions

View File

@ -13,7 +13,8 @@
# by only moving right and down. # by only moving right and down.
import sys import sys
from timeit import default_timer
from projecteuler import timing
def sum_paths(matrix, m, n): def sum_paths(matrix, m, n):
@ -31,9 +32,8 @@ def sum_paths(matrix, m, n):
return matrix[0][0] return matrix[0][0]
def main(): @timing
start = default_timer() def p081():
try: try:
with open('matrix.txt', 'r', encoding='utf-8') as fp: with open('matrix.txt', 'r', encoding='utf-8') as fp:
matrix = fp.readlines() matrix = fp.readlines()
@ -49,13 +49,9 @@ def main():
dist = sum_paths(matrix, 80, 80) dist = sum_paths(matrix, 80, 80)
end = default_timer()
print('Project Euler, Problem 81') print('Project Euler, Problem 81')
print(f'Answer: {dist}') print(f'Answer: {dist}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p081()

View File

@ -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. # 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 import sys
from timeit import default_timer
from numpy import zeros from numpy import zeros
from projecteuler import dijkstra from projecteuler import dijkstra, timing
def main(): @timing
start = default_timer() def p082():
try: try:
with open('matrix.txt', 'r', encoding='utf-8') as fp: with open('matrix.txt', 'r', encoding='utf-8') as fp:
matrix = fp.readlines() matrix = fp.readlines()
@ -43,24 +41,19 @@ def main():
min_path = 999999999 min_path = 999999999
# Use Dijkstra's algorithm starting from all possible nodes # Use Dijkstra's algorithm starting from all possible nodes in the first column.
# in the first column.
for i in range(80): for i in range(80):
dijkstra(matrix, distances, 80, 80, 1, 0, i) dijkstra(matrix, distances, 80, 80, 1, 0, i)
# For the current starting node, check if there is an ending node # For the current starting node, check if there is an ending node
# with a smaller path cost than the current minimum. # with a smaller path cost than the current minimum.
for j in range(80): for j in range(80):
if distances[j][79] < min_path: if distances[j][79] < min_path:
min_path = distances[j][79] min_path = distances[j][79]
end = default_timer()
print('Project Euler, Problem 82') print('Project Euler, Problem 82')
print(f'Answer: {min_path}') print(f'Answer: {min_path}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p082()

View File

@ -15,16 +15,14 @@
# from the top left to the bottom right by moving left, right, up, and down. # from the top left to the bottom right by moving left, right, up, and down.
import sys import sys
from timeit import default_timer
from numpy import zeros from numpy import zeros
from projecteuler import dijkstra from projecteuler import dijkstra, timing
def main(): @timing
start = default_timer() def p083():
try: try:
with open('matrix.txt', 'r', encoding='utf-8') as fp: with open('matrix.txt', 'r', encoding='utf-8') as fp:
matrix = fp.readlines() matrix = fp.readlines()
@ -43,13 +41,9 @@ def main():
dist = distances[79][79] dist = distances[79][79]
end = default_timer()
print('Project Euler, Problem 83') print('Project Euler, Problem 83')
print(f'Answer: {dist}') print(f'Answer: {dist}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p083()

View File

@ -18,7 +18,7 @@
# #
# What is the sum of all the minimal product-sum numbers for 2<=k<=12000? # 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 from projecteuler import sieve, timing

View File

@ -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. # 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 import sys
from timeit import default_timer
from mpmath import matrix from mpmath import matrix
from projecteuler import is_pandigital from projecteuler import is_pandigital, timing
sys.set_int_max_str_digits(70000) sys.set_int_max_str_digits(70000)
def main(): @timing
start = default_timer() def p104():
found = 0 found = 0
fib1 = 1 fib1 = 1
fib2 = 1 fib2 = 1
i = 2 i = 2
while not found: 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 fibn = (fib1 + fib2) % 1000000000
fib1 = fib2 fib1 = fib2
fib2 = fibn fib2 = fibn
i = i + 1 i = i + 1
# If the last 9 digits of fib_n are pandigital, calculate the ith Fibonacci number using # 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 # 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 # 9 digits are correct (we don't need the whole number. If the first 9 digits are also
# pandigital, we found the solution. # pandigital, we found the solution.
if is_pandigital(fibn, 9): if is_pandigital(fibn, 9):
fib_matrix = matrix(2) fib_matrix = matrix(2)
fib_matrix[0, 0] = 1 fib_matrix[0, 0] = 1
@ -52,13 +50,9 @@ def main():
if is_pandigital(int(str(fib)[:9]), 9): if is_pandigital(int(str(fib)[:9]), 9):
found = 1 found = 1
end = default_timer()
print('Project Euler, Problem 104') print('Project Euler, Problem 104')
print(f'Answer: {i}') print(f'Answer: {i}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p104()

View File

@ -8,18 +8,17 @@
# #
# How many reversible numbers are there below one-billion (109)? # How many reversible numbers are there below one-billion (109)?
from timeit import default_timer from projecteuler import timing
def main(): @timing
start = default_timer() def p145():
N = 1000000000 N = 1000000000
count = 0 count = 0
# Brute force approach, sum each number and their reverse and # Brute force approach, sum each number and their reverse and
# check if there are only odd digits. # check if there are only odd digits.
for i in range(11, N): for i in range(11, N):
if i % 10 != 0: if i % 10 != 0:
s = str(i + int(''.join(reversed(str(i))))) s = str(i + int(''.join(reversed(str(i)))))
@ -28,13 +27,9 @@ def main():
'6' not in s and '8' not in s: '6' not in s and '8' not in s:
count = count + 1 count = count + 1
end = default_timer()
print('Project Euler, Problem 145') print('Project Euler, Problem 145')
print(f'Answer: {count}') print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p145()

View File

@ -3,26 +3,25 @@
# Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, # 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. # where each “_” is a single digit.
from timeit import default_timer from projecteuler import timing
def main(): @timing
start = default_timer() def p206():
# Since the square of n has 19 digits, n must be at least 10^9.
# Since the square of n has 19 digits, n must be at least 10^9.
n = 1000000000 n = 1000000000
found = 0 found = 0
while not found: 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 n = n + 10
p = n * n 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: if p % 100 != 0:
continue 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 i = 9
p = p // 100 p = p // 100
@ -35,13 +34,9 @@ def main():
if p == 0: if p == 0:
found = 1 found = 1
end = default_timer()
print('Project Euler, Problem 206') print('Project Euler, Problem 206')
print(f'Answer: {n}') print(f'Answer: {n}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__': if __name__ == '__main__':
main() p206()