Use timing decorator for first 10 problems

This commit is contained in:
daniele 2023-06-07 17:46:10 +02:00
parent 955dc12737
commit 885084211a
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
10 changed files with 65 additions and 123 deletions

View File

@ -4,12 +4,11 @@
#
# Find the sum of all the multiples of 3 or 5 below 1000.
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p001():
sum_ = 0
# 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:
sum_ += i
end = default_timer()
print('Project Euler, Problem 1')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p001()

View File

@ -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.
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p002():
N = 4000000
fib1 = 1
@ -30,13 +28,9 @@ def main():
fib2 = fibn
fibn = fib1 + fib2
end = default_timer()
print('Project Euler, Problem 2')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p002()

View File

@ -4,9 +4,7 @@
#
# What is the largest prime factor of the number 600851475143?
from timeit import default_timer
from projecteuler import is_prime
from projecteuler import is_prime, timing
# Recursive approach: if num is prime, return num, otherwise
@ -23,8 +21,7 @@ def max_prime_factor(num):
i = 3
# If num is divisible by i and i is prime, find largest
# prime factor of num/i.
# If num is divisible by i and i is prime, find largest prime factor of num/i.
while True:
if num % i == 0:
if is_prime(i):
@ -36,18 +33,13 @@ def max_prime_factor(num):
return -1
def main():
start = default_timer()
@timing
def p003():
res = max_prime_factor(600851475143)
end = default_timer()
print('Project Euler, Problem 3')
print(f'Answer: {res}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p003()

View File

@ -4,13 +4,11 @@
#
# Find the largest palindrome made from the product of two 3-digit numbers.
from timeit import default_timer
from projecteuler import is_palindrome
from projecteuler import is_palindrome, timing
def main():
start = default_timer()
@timing
def p004():
max_ = 0
# 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):
max_ = num
end = default_timer()
print('Project Euler, Problem 4')
print(f'Answer: {max_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p004()

View File

@ -4,26 +4,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
from projecteuler import lcmm, timing
def main():
start = default_timer()
@timing
def p005():
values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
# Function define in projecteuler.py to find the least common multiple of multiple numbers.
res = lcmm(values, 20)
end = default_timer()
print('Project Euler, Problem 5')
print(f'Answer: {res}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p005()

View File

@ -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.
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p006():
sum_squares = 0
square_sum = 0
@ -28,13 +27,9 @@ def main():
square_sum = square_sum * square_sum
end = default_timer()
print('Project Euler, Problem 6')
print(f'Answer: {square_sum - sum_squares}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p006()

View File

@ -4,13 +4,11 @@
#
# What is the 10 001st prime number?
from timeit import default_timer
from projecteuler import is_prime
from projecteuler import is_prime, timing
def main():
start = default_timer()
@timing
def p007():
count = 1
n = 1
@ -23,13 +21,9 @@ def main():
if is_prime(n):
count = count + 1
end = default_timer()
print('Project Euler, Problem 7')
print(f'Answer: {n}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p007()

View File

@ -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?
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p008():
number = '73167176531330624919225119674426574742355349194934' +\
'96983520312774506326239578318016984801869478851843' +\
'85861560789112949495459501737958331952853208805511' +\
@ -67,13 +66,9 @@ def main():
if prod > max_:
max_ = prod
end = default_timer()
print('Project Euler, Problem 8')
print(f'Answer: {max_}')
print(f'Elapsed time: {end - start:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()
p008()

View File

@ -11,12 +11,11 @@
# Find the product abc.
from math import gcd
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p009():
found = 0
m = 2
@ -58,13 +57,9 @@ def main():
m = m + 1
end = default_timer()
print('Project Euler, Problem 9')
print(f'Answer: {a * b * c}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p009()

View File

@ -4,13 +4,11 @@
#
# Find the sum of all the primes below two million.
from timeit import default_timer
from projecteuler import sieve
from projecteuler import sieve, timing
def main():
start = default_timer()
@timing
def p010():
N = 2000000
# Use the function in projecteuler.py implementing the
@ -23,13 +21,9 @@ def main():
if primes[i] == 1:
sum_ = sum_ + i
end = default_timer()
print('Project Euler, Problem 10')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p010()