Use timing decorator for first 10 problems
This commit is contained in:
parent
955dc12737
commit
885084211a
@ -4,27 +4,22 @@
|
||||
#
|
||||
# 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,
|
||||
# check if it's a multiple of 3 or 5, if yes add it to the total.
|
||||
# Simple brute-force approach: try every number between 3 and 999,
|
||||
# check if it's a multiple of 3 or 5, if yes add it to the total.
|
||||
for i in range(3, 1000):
|
||||
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()
|
||||
|
@ -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
|
||||
@ -20,8 +18,8 @@ def main():
|
||||
fibn = fib1 + fib2
|
||||
sum_ = 2
|
||||
|
||||
# Simple brute-force approach: generate every value in the Fibonacci
|
||||
# sequence smaller than 4 million and if it's even add it to the total.
|
||||
# Simple brute-force approach: generate every value in the Fibonacci
|
||||
# sequence smaller than 4 million and if it's even add it to the total.
|
||||
while fibn < N:
|
||||
if fibn % 2 == 0:
|
||||
sum_ = sum_ + fibn
|
||||
@ -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()
|
||||
|
@ -4,27 +4,24 @@
|
||||
#
|
||||
# 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
|
||||
# recursively look for the largest prime factor of num divided
|
||||
# by its prime factors until only the largest remains.
|
||||
def max_prime_factor(num):
|
||||
# Use function defined in projecteuler.py to check if a number is prime.
|
||||
# Use function defined in projecteuler.py to check if a number is prime.
|
||||
if is_prime(num):
|
||||
return num
|
||||
|
||||
# If num is even, find the largest prime factor of num/2.
|
||||
# If num is even, find the largest prime factor of num/2.
|
||||
if num % 2 == 0:
|
||||
return max_prime_factor(num // 2)
|
||||
|
||||
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):
|
||||
@ -32,22 +29,17 @@ def max_prime_factor(num):
|
||||
|
||||
i = i + 2
|
||||
|
||||
# Should never get here
|
||||
# Should never get here
|
||||
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()
|
||||
|
@ -4,32 +4,26 @@
|
||||
#
|
||||
# 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
|
||||
# and check if it's palindrome. If the product found is greater than the
|
||||
# current maximum, save the current product.
|
||||
# Using a brute-force approach: generate every product of 3-digit numbers
|
||||
# and check if it's palindrome. If the product found is greater than the
|
||||
# current maximum, save the current product.
|
||||
for i in range(999, 99, -1):
|
||||
for j in range(i, 99, -1):
|
||||
num = i * j
|
||||
# Use the function defined in projecteuler.py to check if a number is palindrome.
|
||||
# Use the function defined in projecteuler.py to check if a number is palindrome.
|
||||
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()
|
||||
|
@ -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.
|
||||
# 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()
|
||||
|
@ -12,29 +12,24 @@
|
||||
#
|
||||
# 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
|
||||
|
||||
# Straightforward brute-force approach.
|
||||
# Straightforward brute-force approach.
|
||||
for i in range(1, 101):
|
||||
sum_squares = sum_squares + i * i
|
||||
square_sum = square_sum + i
|
||||
|
||||
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()
|
||||
|
@ -4,32 +4,26 @@
|
||||
#
|
||||
# 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
|
||||
|
||||
# Brute force approach: start with count=1 and check every odd number
|
||||
# (2 is the only even prime), if it's prime increment count, until the
|
||||
# target prime is reached.
|
||||
# Brute force approach: start with count=1 and check every odd number
|
||||
# (2 is the only even prime), if it's prime increment count, until the
|
||||
# target prime is reached.
|
||||
while count != 10001:
|
||||
n = n + 2
|
||||
# Use the function in projecteuler.py to check if a number is prime.
|
||||
# Use the function in projecteuler.py to check if a number is prime.
|
||||
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()
|
||||
|
@ -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' +\
|
||||
@ -51,12 +50,12 @@ def main():
|
||||
'05886116467109405077541002256983155200055935729725' +\
|
||||
'71636269561882670428252483600823257530420752963450'
|
||||
|
||||
# Transform the string into a list of integers
|
||||
# Transform the string into a list of integers
|
||||
number = list(map(int, number))
|
||||
|
||||
max_ = 0
|
||||
|
||||
# Calculate all the 13-digit products, and save the maximum
|
||||
# Calculate all the 13-digit products, and save the maximum
|
||||
for i in range(1000-13):
|
||||
curr = number[i:i+13]
|
||||
prod = 1
|
||||
@ -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()
|
||||
|
@ -11,18 +11,17 @@
|
||||
# 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
|
||||
|
||||
# Brute force approach: generate all the Pythagorean triplets using
|
||||
# Euclid's formula, until the one where a+b+c=1000 is found.
|
||||
# Brute force approach: generate all the Pythagorean triplets using
|
||||
# Euclid's formula, until the one where a+b+c=1000 is found.
|
||||
while not found:
|
||||
for n in range(1, m):
|
||||
if found == 1:
|
||||
@ -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()
|
||||
|
@ -4,32 +4,26 @@
|
||||
#
|
||||
# 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
|
||||
# Sieve of Eratosthenes algorithm to generate primes.
|
||||
# Use the function in projecteuler.py implementing the
|
||||
# Sieve of Eratosthenes algorithm to generate primes.
|
||||
primes = sieve(N)
|
||||
sum_ = 0
|
||||
|
||||
# Sum all the primes
|
||||
# Sum all the primes
|
||||
for i in range(N):
|
||||
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user