Use timing decorator for last solved problems
This commit is contained in:
parent
e8098ed5e5
commit
8b44d6a68e
@ -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()
|
||||||
|
@ -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,8 +41,7 @@ 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)
|
||||||
|
|
||||||
@ -54,13 +51,9 @@ def main():
|
|||||||
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()
|
||||||
|
@ -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()
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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.
|
# 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
|
||||||
@ -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()
|
||||||
|
@ -8,12 +8,11 @@
|
|||||||
#
|
#
|
||||||
# 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
|
||||||
@ -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()
|
||||||
|
@ -3,12 +3,11 @@
|
|||||||
# 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
|
||||||
@ -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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user