Use timing decorator for problems 41-60

This commit is contained in:
daniele 2023-06-07 20:13:19 +02:00
parent be5e97dfbb
commit 5b99c1ef1c
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
20 changed files with 218 additions and 333 deletions

View File

@ -5,14 +5,11 @@
#
# What is the largest n-digit pandigital prime that exists?
from timeit import default_timer
from projecteuler import is_pandigital, is_prime
from projecteuler import is_pandigital, is_prime, timing
def main():
start = default_timer()
@timing
def p041():
# 8- and 9-digit pandigital numbers can't be prime, because
# 1+2+...+8=36, which is divisible by 3, and 36+9=45 which is
# also divisible by 3, and therefore the whole number is divisible
@ -26,13 +23,9 @@ def main():
# Skipping the even numbers.
i = i - 2
end = default_timer()
print('Project Euler, Problem 41')
print(f'Answer: {i}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p041()

View File

@ -11,7 +11,8 @@
# how many are triangle words?
import sys
from timeit import default_timer
from projecteuler import timing
def is_triang(n):
@ -27,9 +28,8 @@ def is_triang(n):
return False
def main():
start = default_timer()
@timing
def p042():
try:
with open('p042_words.txt', 'r', encoding='utf-8') as fp:
words = list(fp.readline().replace('"', '').split(','))
@ -50,13 +50,9 @@ def main():
if is_triang(value):
count = count + 1
end = default_timer()
print('Project Euler, Problem 42')
print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p042()

View File

@ -16,7 +16,8 @@
# Find the sum of all 0 to 9 pandigital numbers with this property.
from itertools import permutations
from timeit import default_timer
from projecteuler import timing
# Function to check if the value has the desired property.
@ -59,9 +60,8 @@ def has_property(n):
return True
def main():
start = default_timer()
@timing
def p043():
# Find all the permutations
perm = list(permutations('0123456789'))
@ -72,13 +72,9 @@ def main():
if has_property(i):
sum_ = sum_ + int(''.join(map(str, i)))
end = default_timer()
print('Project Euler, Problem 43')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p043()

View File

@ -9,14 +9,11 @@
# Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk Pj| is minimised;
# what is the value of D?
from timeit import default_timer
from projecteuler import is_pentagonal
from projecteuler import is_pentagonal, timing
def main():
start = default_timer()
@timing
def p044():
found = 0
n = 2
@ -35,13 +32,9 @@ def main():
n = n + 1
end = default_timer()
print('Project Euler, Problem 44')
print(f'Answer: {pn - pm}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p044()

View File

@ -10,14 +10,11 @@
#
# Find the next triangle number that is also pentagonal and hexagonal.
from timeit import default_timer
from projecteuler import is_pentagonal
from projecteuler import is_pentagonal, timing
def main():
start = default_timer()
@timing
def p045():
found = 0
i = 143
@ -31,13 +28,9 @@ def main():
if is_pentagonal(n):
found = 1
end = default_timer()
print('Project Euler, Problem 45')
print(f'Answer: {n}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p045()

View File

@ -13,9 +13,7 @@
#
# What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
from timeit import default_timer
from projecteuler import sieve
from projecteuler import sieve, timing
N = 10000
@ -44,9 +42,9 @@ def goldbach(n):
# Return 0 if no solution is found.
return False
def main():
start = default_timer()
@timing
def p046():
found = 0
i = 3
@ -59,13 +57,9 @@ def main():
found = 1
i = i + 2
end = default_timer()
print('Project Euler, Problem 46')
print(f'Answer: {i - 2}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p046()

View File

@ -13,7 +13,7 @@
#
# Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
from timeit import default_timer
from projecteuler import timing
# Function using a modified sieve of Eratosthenes to count
@ -31,9 +31,8 @@ def count_factors(n):
return factors
def main():
start = default_timer()
@timing
def p047():
N = 150000
factors = count_factors(N)
@ -52,13 +51,9 @@ def main():
res = i - 3
break
end = default_timer()
print('Project Euler, Problem 47')
print(f'Answer: {res}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p047()

View File

@ -4,12 +4,11 @@
#
# Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p048():
sum_ = 0
# Simply calculate the sum of the powers
@ -17,13 +16,9 @@ def main():
power = i ** i
sum_ = sum_ + power
end = default_timer()
print('Project Euler, Problem 48')
print(f'Answer: {str(sum_)[-10:]}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p048()

View File

@ -8,14 +8,11 @@
#
# What 12-digit number do you form by concatenating the three terms in this sequence?
from timeit import default_timer
from projecteuler import sieve
from projecteuler import sieve, timing
def main():
start = default_timer()
@timing
def p049():
N = 10000
primes = sieve(N)
@ -30,8 +27,7 @@ def main():
while i < N:
if primes[i] == 1:
for j in range(1, 4255):
# If i, i+j and i+2*j are all primes and they have
# all the same digits, the result has been found.
# If i, i+j and i+2*j are all primes and they have all the same digits, the result has been found.
if i + 2 * j < N and primes[i+j] == 1 and primes[i+2*j] == 1 and\
''.join(sorted(str(i))) == ''.join(sorted(str(i+j))) and\
''.join(sorted(str(i))) == ''.join(sorted(str(i+2*j))):
@ -42,13 +38,9 @@ def main():
i = i + 2
end = default_timer()
print('Project Euler, Problem 49')
print(f'Answer: {str(i)+str(i+j)+str(i+2*j)}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p049()

View File

@ -10,14 +10,11 @@
#
# Which prime, below one-million, can be written as the sum of the most consecutive primes?
from timeit import default_timer
from projecteuler import sieve
from projecteuler import sieve, timing
def main():
start = default_timer()
@timing
def p050():
N = 1000000
primes = sieve(N)
@ -63,13 +60,9 @@ def main():
j = j + 2
end = default_timer()
print('Project Euler, Problem 50')
print(f'Answer: {max_p}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p050()

View File

@ -9,13 +9,10 @@
# Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime
# value family.
from timeit import default_timer
from projecteuler import sieve
from projecteuler import sieve, timing
N = 1000000
# N set to 1000000 as a reasonable limit, which turns out to be enough.
primes = sieve(N)
@ -64,9 +61,9 @@ def replace(n):
return max_
def main():
start = default_timer()
@timing
def p051():
# Checking only odd numbers with at least 4 digits.
i = 1001
@ -83,13 +80,9 @@ def main():
break
i = i + 2
end = default_timer()
print('Project Euler, Problem 51')
print(f'Answer: {i}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p051()

View File

@ -4,12 +4,11 @@
#
# Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p052():
i = 1
# Brute force approach, try every integer number until the desired one is found.
@ -22,13 +21,9 @@ def main():
break
i = i + 1
end = default_timer()
print('Project Euler, Problem 52')
print(f'Answer: {i}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p052()

View File

@ -12,14 +12,13 @@
#
# How many, not necessarily distinct, values of (n r) for 1≤n≤100, are greater than one-million?
from timeit import default_timer
from scipy.special import comb
from projecteuler import timing
def main():
start = default_timer()
@timing
def p053():
LIMIT = 1000000
count = 0
@ -30,13 +29,9 @@ def main():
if comb(i, j) > LIMIT:
count = count + 1
end = default_timer()
print('Project Euler, Problem 53')
print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p053()

View File

@ -48,7 +48,8 @@
import sys
from enum import IntEnum
from timeit import default_timer
from projecteuler import timing
class Value(IntEnum):
@ -111,7 +112,7 @@ class Hand():
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cards = list()
self.cards = []
def sort(self):
self.cards.sort(key=lambda x: x.value)
@ -126,7 +127,6 @@ class Game():
self.hand2 = None
def play(self):
# If player 1 has a Royal Flush, player 1 wins.
if self.hand1.cards[4].value == Value.Ace and self.hand1.cards[3].value == Value.King and\
self.hand1.cards[2].value == Value.Queen and self.hand1.cards[1].value == Value.Jack and\
@ -241,8 +241,7 @@ class Game():
if not full1 and full2:
return -1
# If both players have a full house, check who has the highest value
# for the three equal cards (the third card in the array will be part
# If both players have a full house, check who has the highest value for the three equal cards (the third card in the array will be part
# of the set of three).
if full1 and full2:
if self.hand1.cards[2].value > self.hand2.cards[2].value:
@ -443,9 +442,8 @@ class Game():
return 0
def main():
start = default_timer()
@timing
def p054():
try:
with open('p054_poker.txt', 'r', encoding='utf-8') as fp:
games = fp.readlines()
@ -476,7 +474,7 @@ def main():
hand1.sort()
hand2.sort()
for k in hand1.cards:
for _ in hand1.cards:
game = Game()
game.hand1 = hand1
game.hand2 = hand2
@ -484,13 +482,9 @@ def main():
if game.play() == 1:
count = count + 1
end = default_timer()
print('Project Euler, Problem 54')
print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p054()

View File

@ -23,9 +23,7 @@
#
# NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.
from timeit import default_timer
from projecteuler import is_palindrome
from projecteuler import is_palindrome, timing
def is_lychrel(n):
@ -53,9 +51,8 @@ def is_lychrel(n):
return True
def main():
start = default_timer()
@timing
def p055():
count = 0
# For each number, use the is_lychrel function to check if the number
@ -64,13 +61,9 @@ def main():
if is_lychrel(i):
count = count + 1
end = default_timer()
print('Project Euler, Problem 55')
print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p055()

View File

@ -5,12 +5,11 @@
#
# Considering natural numbers of the form, a^b, where a, b < 100, what is the maximum digital sum?
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p056():
max_ = 0
# Straightforward brute force approach
@ -26,13 +25,9 @@ def main():
if sum_ > max_:
max_ = sum_
end = default_timer()
print('Project Euler, Problem 56')
print(f'Answer: {max_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p056()

View File

@ -16,12 +16,11 @@
#
# In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator?
from timeit import default_timer
from projecteuler import timing
def main():
start = default_timer()
@timing
def p057():
n = 1
d = 1
count = 0
@ -36,13 +35,9 @@ def main():
if len(str(n)) > len(str(d)):
count = count + 1
end = default_timer()
print('Project Euler, Problem 57')
print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p057()

View File

@ -16,14 +16,11 @@
# If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued,
# what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?
from timeit import default_timer
from projecteuler import is_prime
from projecteuler import is_prime, timing
def main():
start = default_timer()
@timing
def p058():
# Starting with 1, the next four numbers in the diagonal are 3 (1+2), 5 (1+2+2), 7 (1+2+2+2)
# and 9 (1+2+2+2+2). Check which are prime, increment the counter every time a new prime is
# found, and divide by the number of elements of the diagonal, which are increase by 4 at
@ -61,13 +58,9 @@ def main():
if ratio < 0.1:
break
end = default_timer()
print('Project Euler, Problem 58')
print(f'Answer: {l}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p058()

View File

@ -18,7 +18,9 @@
# encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the
# ASCII values in the original text.
from timeit import default_timer
import sys
from projecteuler import timing
class EncryptedText():
@ -40,6 +42,8 @@ class EncryptedText():
self.len = len(self.text)
return 0
def decrypt(self):
found = 0
@ -77,9 +81,8 @@ class EncryptedText():
return plain_text
def main():
start = default_timer()
@timing
def p059():
enc_text = EncryptedText()
if enc_text.read_text('p059_cipher.txt') == -1:
@ -92,13 +95,9 @@ def main():
for i in list(plain_text):
sum_ = sum_ + ord(i)
end = default_timer()
print('Project Euler, Problem 59')
print(f'Answer: {sum_}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p059()

View File

@ -6,14 +6,11 @@
#
# Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
from timeit import default_timer
from projecteuler import sieve, is_prime
from projecteuler import is_prime, sieve, timing
def main():
start = default_timer()
@timing
def p060():
N = 10000
primes = sieve(N)
@ -85,13 +82,9 @@ def main():
p1 = p1 + 2
end = default_timer()
print('Project Euler, Problem 60')
print(f'Answer: {n}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()
p060()