Use timing decorator for problems 71-80
This commit is contained in:
parent
f54083389b
commit
e8098ed5e5
@ -12,12 +12,12 @@
|
||||
# of the fraction immediately to the left of 3/7.
|
||||
|
||||
from math import gcd
|
||||
from timeit import default_timer
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p071():
|
||||
N = 1000000
|
||||
|
||||
max_ = 0
|
||||
@ -43,13 +43,9 @@ def main():
|
||||
|
||||
max_n = n
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 71')
|
||||
print(f'Answer: {max_n}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p071()
|
||||
|
@ -10,14 +10,11 @@
|
||||
#
|
||||
# How many elements would be contained in the set of reduced proper fractions for d ≤ 1,000,000?
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
from projecteuler import sieve, phi
|
||||
from projecteuler import sieve, phi, timing
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p072():
|
||||
N = 1000001
|
||||
|
||||
count = 0
|
||||
@ -30,13 +27,9 @@ def main():
|
||||
for i in range(2, N):
|
||||
count = count + phi(i, primes)
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 72')
|
||||
print(f'Answer: {count}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
print(f'Answer: {int(count)}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p072()
|
||||
|
@ -11,12 +11,12 @@
|
||||
# How many fractions lie between 1/3 and 1/2 in the sorted set of reduced proper fractions for d ≤ 12,000?
|
||||
|
||||
from math import gcd
|
||||
from timeit import default_timer
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p073():
|
||||
count = 0
|
||||
|
||||
# For each denominator q, we need to find the fractions p/q for which
|
||||
@ -30,13 +30,9 @@ def main():
|
||||
if gcd(j, i) == 1:
|
||||
count = count + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 73')
|
||||
print(f'Answer: {count}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p073()
|
||||
|
@ -23,7 +23,8 @@
|
||||
# How many chains, with a starting number below one million, contain exactly sixty non-repeating terms?
|
||||
|
||||
from math import factorial
|
||||
from timeit import default_timer
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
N = 1000000
|
||||
@ -44,21 +45,21 @@ def len_chain(n):
|
||||
|
||||
# Generate the next number of the chain by taking
|
||||
# the digits of the current value, calculating the
|
||||
# factorials and adding them.*/
|
||||
# factorials and adding them.
|
||||
while value != 0:
|
||||
tmp = tmp + factorial(value % 10)
|
||||
value = value // 10
|
||||
|
||||
# If the chain length for the new value has already been
|
||||
# calculated before, use the saved value (only chains for
|
||||
# values smaller than N are saved).*/
|
||||
# values smaller than N are saved).
|
||||
if tmp < N and chains[tmp] != 0:
|
||||
return count + chains[tmp]
|
||||
|
||||
value = tmp
|
||||
|
||||
# If the current value is already present in the chain,
|
||||
# the chain is finished.*/
|
||||
# the chain is finished.
|
||||
for i in range(count):
|
||||
if chain[i] == value:
|
||||
finished = 1
|
||||
@ -70,9 +71,8 @@ def len_chain(n):
|
||||
return count
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p074():
|
||||
count = 0
|
||||
|
||||
# Simple brute force approach, for every number calculate
|
||||
@ -81,13 +81,9 @@ def main():
|
||||
if len_chain(i) == 60:
|
||||
count = count + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 74')
|
||||
print(f'Answer: {count}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p074()
|
||||
|
@ -18,12 +18,12 @@
|
||||
# Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can exactly one integer sided right angle triangle be formed?
|
||||
|
||||
from math import gcd
|
||||
from timeit import default_timer
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p075():
|
||||
N = 1500000
|
||||
|
||||
l = [0] * (N+1)
|
||||
@ -68,13 +68,9 @@ def main():
|
||||
if l[i] == 1:
|
||||
count = count + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 75')
|
||||
print(f'Answer: {count}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p075()
|
||||
|
@ -11,14 +11,11 @@
|
||||
#
|
||||
# How many different ways can one hundred be written as a sum of at least two positive integers?*/
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
from projecteuler import partition_fn
|
||||
from projecteuler import partition_fn, timing
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p076():
|
||||
partitions = [0] * 101
|
||||
|
||||
# The number of ways a number can be written as a sum is given by the partition function
|
||||
@ -26,13 +23,9 @@ def main():
|
||||
# The function is implemented in projecteuler.py.
|
||||
n = partition_fn(100, partitions) - 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 76')
|
||||
print(f'Answer: {n}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p076()
|
||||
|
@ -10,16 +10,13 @@
|
||||
#
|
||||
# What is the first value which can be written as the sum of primes in over five thousand different ways?
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
from projecteuler import is_prime
|
||||
from projecteuler import is_prime, timing
|
||||
|
||||
|
||||
primes = [0] * 100
|
||||
|
||||
|
||||
# Function using a simple recursive brute force approach
|
||||
# to find all the partitions.
|
||||
# Function using a simple recursive brute force approach to find all the partitions.
|
||||
def count(value, n, i, target):
|
||||
for j in range(i, 100):
|
||||
value = value + primes[j]
|
||||
@ -36,9 +33,8 @@ def count(value, n, i, target):
|
||||
return n
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p077():
|
||||
# Generate a list of the first 100 primes.
|
||||
i = 0
|
||||
j = 0
|
||||
@ -62,13 +58,9 @@ def main():
|
||||
|
||||
i = i + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 77')
|
||||
print(f'Answer: {i}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p077()
|
||||
|
@ -13,32 +13,24 @@
|
||||
#
|
||||
# Find the least value of n for which p(n) is divisible by one million.
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
from projecteuler import partition_fn
|
||||
from projecteuler import partition_fn, timing
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p078():
|
||||
N = 1000000
|
||||
|
||||
partitions = [0] * N
|
||||
|
||||
i = 0
|
||||
|
||||
# Using the partition function to calculate the number of partitions,
|
||||
# giving the result modulo N.*/
|
||||
# Using the partition function to calculate the number of partitions, giving the result modulo N.
|
||||
while partition_fn(i, partitions, N) != 0:
|
||||
i = i + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 78')
|
||||
print(f'Answer: {i}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p078()
|
||||
|
@ -10,12 +10,12 @@
|
||||
|
||||
import sys
|
||||
from itertools import permutations
|
||||
from timeit import default_timer
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
def check_passcode(passcode, len_, logins, n):
|
||||
# For every login attempt, check if all the digits appear in the
|
||||
# passcode in the correct order. Return 0 if a login attempt
|
||||
# For every login attempt, check if all the digits appear in the passcode in the correct order. Return 0 if a login attempt
|
||||
# incompatible with the current passcode is found.
|
||||
for i in range(n):
|
||||
k = 0
|
||||
@ -32,9 +32,8 @@ def check_passcode(passcode, len_, logins, n):
|
||||
return 1
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p079():
|
||||
try:
|
||||
with open('p079_keylog.txt', 'r', encoding='utf-8') as fp:
|
||||
logins = fp.readlines()
|
||||
@ -58,8 +57,7 @@ def main():
|
||||
|
||||
j = 0
|
||||
for i in range(10):
|
||||
# To generate the passcode, only use the digits present in the
|
||||
# login attempts.
|
||||
# To generate the passcode, only use the digits present in the login attempts.
|
||||
if digits[i] > 0:
|
||||
passcode_digits[j] = i
|
||||
j = j + 1
|
||||
@ -70,8 +68,7 @@ def main():
|
||||
while not found:
|
||||
passcode = [0] * len_
|
||||
|
||||
# For the current length, generate the first passcode with the
|
||||
# digits in order.
|
||||
# For the current length, generate the first passcode with the digits in order.
|
||||
for i in range(len_):
|
||||
passcode[i] = passcode_digits[i]
|
||||
|
||||
@ -93,13 +90,9 @@ def main():
|
||||
# If the passcode has not yet been found, try a longer passcode.
|
||||
len_ = len_ + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 79')
|
||||
print(f'Answer: {res}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p079()
|
||||
|
@ -8,9 +8,10 @@
|
||||
# For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits
|
||||
# for all the irrational square roots
|
||||
|
||||
from timeit import default_timer
|
||||
from mpmath import sqrt, mp
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
def is_square(n):
|
||||
p = sqrt(n)
|
||||
@ -19,9 +20,8 @@ def is_square(n):
|
||||
return bool(p == m)
|
||||
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
@timing
|
||||
def p080():
|
||||
# Set the precision to 100 digits
|
||||
mp.dps = 102
|
||||
|
||||
@ -29,8 +29,7 @@ def main():
|
||||
|
||||
for i in range(2, 100):
|
||||
if not is_square(i):
|
||||
# Calculate the square root of the current number with the given precision
|
||||
# and sum the digits to the total.
|
||||
# Calculate the square root of the current number with the given precision and sum the digits to the total.
|
||||
root = str(sqrt(i))
|
||||
|
||||
sum_ = sum_ + int(root[0])
|
||||
@ -38,13 +37,9 @@ def main():
|
||||
for j in range(2, 101):
|
||||
sum_ = sum_ + int(root[j])
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 80')
|
||||
print(f'Answer: {sum_}')
|
||||
|
||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
p080()
|
||||
|
Loading…
x
Reference in New Issue
Block a user