Use timing decorator for problems 11-20
This commit is contained in:
parent
885084211a
commit
634eb3f750
@ -27,12 +27,11 @@
|
|||||||
#
|
#
|
||||||
# What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
|
# What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p011():
|
||||||
|
|
||||||
grid = [[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
|
grid = [[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
|
||||||
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
|
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
|
||||||
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
|
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
|
||||||
@ -110,13 +109,9 @@ def main():
|
|||||||
if prod > max_:
|
if prod > max_:
|
||||||
max_ = prod
|
max_ = prod
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 11')
|
print('Project Euler, Problem 11')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {max_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p011()
|
||||||
|
@ -19,13 +19,11 @@
|
|||||||
#
|
#
|
||||||
# What is the value of the first triangle number to have over five hundred divisors?
|
# What is the value of the first triangle number to have over five hundred divisors?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import count_divisors, timing
|
||||||
from projecteuler import count_divisors
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p012():
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
triang = 0
|
triang = 0
|
||||||
finished = 0
|
finished = 0
|
||||||
@ -39,13 +37,9 @@ def main():
|
|||||||
if count_divisors(triang) > 500:
|
if count_divisors(triang) > 500:
|
||||||
finished = 1
|
finished = 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 12')
|
print('Project Euler, Problem 12')
|
||||||
print(f'Answer: {triang}')
|
print(f'Answer: {triang}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p012()
|
||||||
|
@ -103,13 +103,13 @@
|
|||||||
# 20849603980134001723930671666823555245252804609722
|
# 20849603980134001723930671666823555245252804609722
|
||||||
# 53503534226472524250874054075591789781264330331690
|
# 53503534226472524250874054075591789781264330331690
|
||||||
|
|
||||||
from timeit import default_timer
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
def main():
|
|
||||||
start = default_timer()
|
|
||||||
|
|
||||||
|
@timing
|
||||||
|
def p013():
|
||||||
numbers = [37107287533902102798797998220837590246510135740250,
|
numbers = [37107287533902102798797998220837590246510135740250,
|
||||||
46376937677490009712648124896970078050417018260538,
|
46376937677490009712648124896970078050417018260538,
|
||||||
74324986199524741059474233309513058123726617309629,
|
74324986199524741059474233309513058123726617309629,
|
||||||
@ -216,13 +216,9 @@ def main():
|
|||||||
|
|
||||||
sum_ = str(numbers.sum())
|
sum_ = str(numbers.sum())
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 13')
|
print('Project Euler, Problem 13')
|
||||||
print(f'Answer: {sum_[:10]}')
|
print(f'Answer: {sum_[:10]}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p013()
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
#
|
#
|
||||||
# NOTE: Once the chain starts the terms are allowed to go above one million.
|
# NOTE: Once the chain starts the terms are allowed to go above one million.
|
||||||
|
|
||||||
from timeit import default_timer
|
|
||||||
from numpy import zeros
|
from numpy import zeros
|
||||||
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
N = 1000000
|
N = 1000000
|
||||||
collatz_found = zeros(N, dtype=int)
|
collatz_found = zeros(N, dtype=int)
|
||||||
@ -42,9 +43,9 @@ def collatz_length(n):
|
|||||||
|
|
||||||
return 1 + collatz_length(3*n+1)
|
return 1 + collatz_length(3*n+1)
|
||||||
|
|
||||||
def main():
|
|
||||||
start = default_timer()
|
|
||||||
|
|
||||||
|
@timing
|
||||||
|
def p014():
|
||||||
max_l = 0
|
max_l = 0
|
||||||
max_ = 0
|
max_ = 0
|
||||||
|
|
||||||
@ -58,13 +59,9 @@ def main():
|
|||||||
max_l = count
|
max_l = count
|
||||||
max_ = i
|
max_ = i
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 14')
|
print('Project Euler, Problem 14')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {max_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p014()
|
||||||
|
@ -4,12 +4,12 @@
|
|||||||
# How many such routes are there through a 20×20 grid?
|
# How many such routes are there through a 20×20 grid?
|
||||||
|
|
||||||
from math import factorial
|
from math import factorial
|
||||||
from timeit import default_timer
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p015():
|
||||||
|
|
||||||
# Using a combinatorial solution: in a 20x20 grid there will always be
|
# Using a combinatorial solution: in a 20x20 grid there will always be
|
||||||
# 20 movements to the right and 20 movements down, that can be represented
|
# 20 movements to the right and 20 movements down, that can be represented
|
||||||
# as a string of Rs and Ds. The number of routes is the number of combinations.
|
# as a string of Rs and Ds. The number of routes is the number of combinations.
|
||||||
@ -19,13 +19,9 @@ def main():
|
|||||||
tmp = tmp * tmp
|
tmp = tmp * tmp
|
||||||
count = count // tmp
|
count = count // tmp
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 15')
|
print('Project Euler, Problem 15')
|
||||||
print(f'Answer: {count}')
|
print(f'Answer: {count}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p015()
|
||||||
|
@ -4,12 +4,11 @@
|
|||||||
#
|
#
|
||||||
# What is the sum of the digits of the number 2^1000?
|
# What is the sum of the digits of the number 2^1000?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p016():
|
||||||
|
|
||||||
# Simply calculate 2^1000, convert the result to string and calculate
|
# Simply calculate 2^1000, convert the result to string and calculate
|
||||||
# the sum of the digits
|
# the sum of the digits
|
||||||
res = str(2 ** 1000)
|
res = str(2 ** 1000)
|
||||||
@ -19,13 +18,9 @@ def main():
|
|||||||
for i in res:
|
for i in res:
|
||||||
sum_ = sum_ + int(i)
|
sum_ = sum_ + int(i)
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 16')
|
print('Project Euler, Problem 16')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p016()
|
||||||
|
@ -7,12 +7,11 @@
|
|||||||
# NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen)
|
# NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen)
|
||||||
# contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
|
# contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p017():
|
||||||
|
|
||||||
# First list contains number of letters for numbers from 1 to 19,
|
# First list contains number of letters for numbers from 1 to 19,
|
||||||
# the second letters for "twenty", "thirty", ..., "ninety",
|
# the second letters for "twenty", "thirty", ..., "ninety",
|
||||||
# the third letters for "one hundred and", "two hundred and", ..., "nine hundre and",
|
# the third letters for "one hundred and", "two hundred and", ..., "nine hundre and",
|
||||||
@ -37,6 +36,7 @@ def main():
|
|||||||
# Add "one", "two", ..., "nine".
|
# Add "one", "two", ..., "nine".
|
||||||
for j in range(9):
|
for j in range(9):
|
||||||
n_letters[1][i] = n_letters[1][i] + n_letters[0][j]
|
n_letters[1][i] = n_letters[1][i] + n_letters[0][j]
|
||||||
|
|
||||||
sum_ = sum_ + n_letters[1][i]
|
sum_ = sum_ + n_letters[1][i]
|
||||||
|
|
||||||
# Add the letters of the numbers from 100 to 999.
|
# Add the letters of the numbers from 100 to 999.
|
||||||
@ -57,13 +57,9 @@ def main():
|
|||||||
# Add "one thousand".
|
# Add "one thousand".
|
||||||
sum_ = sum_ + n_letters[3][0]
|
sum_ = sum_ + n_letters[3][0]
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 17')
|
print('Project Euler, Problem 17')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p017()
|
||||||
|
@ -31,14 +31,12 @@
|
|||||||
# with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
|
# with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from timeit import default_timer
|
|
||||||
|
|
||||||
from projecteuler import find_max_path
|
from projecteuler import find_max_path, timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p018():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open('p018_triangle.txt', 'r', encoding='utf-8') as fp:
|
with open('p018_triangle.txt', 'r', encoding='utf-8') as fp:
|
||||||
triang = []
|
triang = []
|
||||||
@ -57,13 +55,9 @@ def main():
|
|||||||
# Use the function implemented in projecteuler.c to find the maximum path.
|
# Use the function implemented in projecteuler.c to find the maximum path.
|
||||||
max_ = find_max_path(triang, 15)
|
max_ = find_max_path(triang, 15)
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 18')
|
print('Project Euler, Problem 18')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {max_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p018()
|
||||||
|
@ -14,12 +14,12 @@
|
|||||||
# How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
|
# How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from timeit import default_timer
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p019():
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
# Use the datetime library to find out which first day of the month is a Sunday
|
# Use the datetime library to find out which first day of the month is a Sunday
|
||||||
@ -28,13 +28,9 @@ def main():
|
|||||||
if datetime.datetime(year, month, 1).weekday() == 6:
|
if datetime.datetime(year, month, 1).weekday() == 6:
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 19')
|
print('Project Euler, Problem 19')
|
||||||
print(f'Answer: {count}')
|
print(f'Answer: {count}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p019()
|
||||||
|
@ -8,12 +8,12 @@
|
|||||||
# Find the sum of the digits in the number 100!
|
# Find the sum of the digits in the number 100!
|
||||||
|
|
||||||
from math import factorial
|
from math import factorial
|
||||||
from timeit import default_timer
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p020():
|
||||||
|
|
||||||
# Calculate the factorial, convert the result to string and sum the digits.
|
# Calculate the factorial, convert the result to string and sum the digits.
|
||||||
n = str(factorial(100))
|
n = str(factorial(100))
|
||||||
|
|
||||||
@ -22,13 +22,9 @@ def main():
|
|||||||
for i in n:
|
for i in n:
|
||||||
sum_ = sum_ + int(i)
|
sum_ = sum_ + int(i)
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 20')
|
print('Project Euler, Problem 20')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p020()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user