Use timing decorator for first 10 problems
This commit is contained in:
parent
955dc12737
commit
885084211a
@ -4,12 +4,11 @@
|
|||||||
#
|
#
|
||||||
# Find the sum of all the multiples of 3 or 5 below 1000.
|
# Find the sum of all the multiples of 3 or 5 below 1000.
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p001():
|
||||||
|
|
||||||
sum_ = 0
|
sum_ = 0
|
||||||
|
|
||||||
# Simple brute-force approach: try every number between 3 and 999,
|
# Simple brute-force approach: try every number between 3 and 999,
|
||||||
@ -18,13 +17,9 @@ def main():
|
|||||||
if i % 3 == 0 or i % 5 == 0:
|
if i % 3 == 0 or i % 5 == 0:
|
||||||
sum_ += i
|
sum_ += i
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 1')
|
print('Project Euler, Problem 1')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p001()
|
||||||
|
@ -6,13 +6,11 @@
|
|||||||
#
|
#
|
||||||
# By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
|
# By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
|
def p002():
|
||||||
start = default_timer()
|
|
||||||
|
|
||||||
N = 4000000
|
N = 4000000
|
||||||
|
|
||||||
fib1 = 1
|
fib1 = 1
|
||||||
@ -30,13 +28,9 @@ def main():
|
|||||||
fib2 = fibn
|
fib2 = fibn
|
||||||
fibn = fib1 + fib2
|
fibn = fib1 + fib2
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 2')
|
print('Project Euler, Problem 2')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p002()
|
||||||
|
@ -4,9 +4,7 @@
|
|||||||
#
|
#
|
||||||
# What is the largest prime factor of the number 600851475143?
|
# What is the largest prime factor of the number 600851475143?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import is_prime, timing
|
||||||
|
|
||||||
from projecteuler import is_prime
|
|
||||||
|
|
||||||
|
|
||||||
# Recursive approach: if num is prime, return num, otherwise
|
# Recursive approach: if num is prime, return num, otherwise
|
||||||
@ -23,8 +21,7 @@ def max_prime_factor(num):
|
|||||||
|
|
||||||
i = 3
|
i = 3
|
||||||
|
|
||||||
# If num is divisible by i and i is prime, find largest
|
# If num is divisible by i and i is prime, find largest prime factor of num/i.
|
||||||
# prime factor of num/i.
|
|
||||||
while True:
|
while True:
|
||||||
if num % i == 0:
|
if num % i == 0:
|
||||||
if is_prime(i):
|
if is_prime(i):
|
||||||
@ -36,18 +33,13 @@ def max_prime_factor(num):
|
|||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p003():
|
||||||
|
|
||||||
res = max_prime_factor(600851475143)
|
res = max_prime_factor(600851475143)
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 3')
|
print('Project Euler, Problem 3')
|
||||||
print(f'Answer: {res}')
|
print(f'Answer: {res}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p003()
|
||||||
|
@ -4,13 +4,11 @@
|
|||||||
#
|
#
|
||||||
# Find the largest palindrome made from the product of two 3-digit numbers.
|
# Find the largest palindrome made from the product of two 3-digit numbers.
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import is_palindrome, timing
|
||||||
from projecteuler import is_palindrome
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p004():
|
||||||
|
|
||||||
max_ = 0
|
max_ = 0
|
||||||
|
|
||||||
# Using a brute-force approach: generate every product of 3-digit numbers
|
# Using a brute-force approach: generate every product of 3-digit numbers
|
||||||
@ -23,13 +21,9 @@ def main():
|
|||||||
if num > max_ and is_palindrome(num, 10):
|
if num > max_ and is_palindrome(num, 10):
|
||||||
max_ = num
|
max_ = num
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 4')
|
print('Project Euler, Problem 4')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {max_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p004()
|
||||||
|
@ -4,26 +4,20 @@
|
|||||||
#
|
#
|
||||||
# What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
# What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import lcmm, timing
|
||||||
from projecteuler import lcmm
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p005():
|
||||||
|
|
||||||
values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
||||||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
|
11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
|
||||||
|
|
||||||
# Function define in projecteuler.py to find the least common multiple of multiple numbers.
|
# Function define in projecteuler.py to find the least common multiple of multiple numbers.
|
||||||
res = lcmm(values, 20)
|
res = lcmm(values, 20)
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 5')
|
print('Project Euler, Problem 5')
|
||||||
print(f'Answer: {res}')
|
print(f'Answer: {res}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p005()
|
||||||
|
@ -12,12 +12,11 @@
|
|||||||
#
|
#
|
||||||
# Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
|
# Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p006():
|
||||||
|
|
||||||
sum_squares = 0
|
sum_squares = 0
|
||||||
square_sum = 0
|
square_sum = 0
|
||||||
|
|
||||||
@ -28,13 +27,9 @@ def main():
|
|||||||
|
|
||||||
square_sum = square_sum * square_sum
|
square_sum = square_sum * square_sum
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 6')
|
print('Project Euler, Problem 6')
|
||||||
print(f'Answer: {square_sum - sum_squares}')
|
print(f'Answer: {square_sum - sum_squares}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p006()
|
||||||
|
@ -4,13 +4,11 @@
|
|||||||
#
|
#
|
||||||
# What is the 10 001st prime number?
|
# What is the 10 001st prime number?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import is_prime, timing
|
||||||
from projecteuler import is_prime
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p007():
|
||||||
|
|
||||||
count = 1
|
count = 1
|
||||||
n = 1
|
n = 1
|
||||||
|
|
||||||
@ -23,13 +21,9 @@ def main():
|
|||||||
if is_prime(n):
|
if is_prime(n):
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 7')
|
print('Project Euler, Problem 7')
|
||||||
print(f'Answer: {n}')
|
print(f'Answer: {n}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p007()
|
||||||
|
@ -25,12 +25,11 @@
|
|||||||
#
|
#
|
||||||
# Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
|
# Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p008():
|
||||||
|
|
||||||
number = '73167176531330624919225119674426574742355349194934' +\
|
number = '73167176531330624919225119674426574742355349194934' +\
|
||||||
'96983520312774506326239578318016984801869478851843' +\
|
'96983520312774506326239578318016984801869478851843' +\
|
||||||
'85861560789112949495459501737958331952853208805511' +\
|
'85861560789112949495459501737958331952853208805511' +\
|
||||||
@ -67,13 +66,9 @@ def main():
|
|||||||
if prod > max_:
|
if prod > max_:
|
||||||
max_ = prod
|
max_ = prod
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 8')
|
print('Project Euler, Problem 8')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {max_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds'.format(end - start))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p008()
|
||||||
|
@ -11,12 +11,11 @@
|
|||||||
# Find the product abc.
|
# Find the product abc.
|
||||||
|
|
||||||
from math import gcd
|
from math import gcd
|
||||||
from timeit import default_timer
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p009():
|
||||||
|
|
||||||
found = 0
|
found = 0
|
||||||
|
|
||||||
m = 2
|
m = 2
|
||||||
@ -58,13 +57,9 @@ def main():
|
|||||||
|
|
||||||
m = m + 1
|
m = m + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 9')
|
print('Project Euler, Problem 9')
|
||||||
print(f'Answer: {a * b * c}')
|
print(f'Answer: {a * b * c}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p009()
|
||||||
|
@ -4,13 +4,11 @@
|
|||||||
#
|
#
|
||||||
# Find the sum of all the primes below two million.
|
# Find the sum of all the primes below two million.
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import sieve, timing
|
||||||
from projecteuler import sieve
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p010():
|
||||||
|
|
||||||
N = 2000000
|
N = 2000000
|
||||||
|
|
||||||
# Use the function in projecteuler.py implementing the
|
# Use the function in projecteuler.py implementing the
|
||||||
@ -23,13 +21,9 @@ def main():
|
|||||||
if primes[i] == 1:
|
if primes[i] == 1:
|
||||||
sum_ = sum_ + i
|
sum_ = sum_ + i
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 10')
|
print('Project Euler, Problem 10')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p010()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user