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.
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()

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.
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,8 +41,7 @@ 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)
@ -54,13 +51,9 @@ def main():
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()

View File

@ -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()

View File

@ -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

View File

@ -10,19 +10,17 @@
# 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
@ -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()

View File

@ -8,12 +8,11 @@
#
# 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
@ -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()

View File

@ -3,12 +3,11 @@
# 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()
@timing
def p206():
# Since the square of n has 19 digits, n must be at least 10^9.
n = 1000000000
found = 0
@ -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()