Use timing decorator for problems 41-60
This commit is contained in:
parent
be5e97dfbb
commit
5b99c1ef1c
@ -5,14 +5,11 @@
|
|||||||
#
|
#
|
||||||
# What is the largest n-digit pandigital prime that exists?
|
# What is the largest n-digit pandigital prime that exists?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import is_pandigital, is_prime, timing
|
||||||
|
|
||||||
from projecteuler import is_pandigital, is_prime
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p041():
|
||||||
|
|
||||||
# 8- and 9-digit pandigital numbers can't be prime, because
|
# 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
|
# 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
|
# also divisible by 3, and therefore the whole number is divisible
|
||||||
@ -26,13 +23,9 @@ def main():
|
|||||||
# Skipping the even numbers.
|
# Skipping the even numbers.
|
||||||
i = i - 2
|
i = i - 2
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 41')
|
print('Project Euler, Problem 41')
|
||||||
print(f'Answer: {i}')
|
print(f'Answer: {i}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p041()
|
||||||
|
@ -11,7 +11,8 @@
|
|||||||
# how many are triangle words?
|
# how many are triangle words?
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from timeit import default_timer
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
def is_triang(n):
|
def is_triang(n):
|
||||||
@ -27,9 +28,8 @@ def is_triang(n):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p042():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open('p042_words.txt', 'r', encoding='utf-8') as fp:
|
with open('p042_words.txt', 'r', encoding='utf-8') as fp:
|
||||||
words = list(fp.readline().replace('"', '').split(','))
|
words = list(fp.readline().replace('"', '').split(','))
|
||||||
@ -50,13 +50,9 @@ def main():
|
|||||||
if is_triang(value):
|
if is_triang(value):
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 42')
|
print('Project Euler, Problem 42')
|
||||||
print(f'Answer: {count}')
|
print(f'Answer: {count}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p042()
|
||||||
|
@ -16,7 +16,8 @@
|
|||||||
# Find the sum of all 0 to 9 pandigital numbers with this property.
|
# Find the sum of all 0 to 9 pandigital numbers with this property.
|
||||||
|
|
||||||
from itertools import permutations
|
from itertools import permutations
|
||||||
from timeit import default_timer
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
# Function to check if the value has the desired property.
|
# Function to check if the value has the desired property.
|
||||||
@ -59,9 +60,8 @@ def has_property(n):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p043():
|
||||||
|
|
||||||
# Find all the permutations
|
# Find all the permutations
|
||||||
perm = list(permutations('0123456789'))
|
perm = list(permutations('0123456789'))
|
||||||
|
|
||||||
@ -72,13 +72,9 @@ def main():
|
|||||||
if has_property(i):
|
if has_property(i):
|
||||||
sum_ = sum_ + int(''.join(map(str, i)))
|
sum_ = sum_ + int(''.join(map(str, i)))
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 43')
|
print('Project Euler, Problem 43')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p043()
|
||||||
|
@ -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;
|
# 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?
|
# what is the value of D?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import is_pentagonal, timing
|
||||||
|
|
||||||
from projecteuler import is_pentagonal
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p044():
|
||||||
|
|
||||||
found = 0
|
found = 0
|
||||||
n = 2
|
n = 2
|
||||||
|
|
||||||
@ -35,13 +32,9 @@ def main():
|
|||||||
|
|
||||||
n = n + 1
|
n = n + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 44')
|
print('Project Euler, Problem 44')
|
||||||
print(f'Answer: {pn - pm}')
|
print(f'Answer: {pn - pm}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p044()
|
||||||
|
@ -10,14 +10,11 @@
|
|||||||
#
|
#
|
||||||
# Find the next triangle number that is also pentagonal and hexagonal.
|
# Find the next triangle number that is also pentagonal and hexagonal.
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import is_pentagonal, timing
|
||||||
|
|
||||||
from projecteuler import is_pentagonal
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p045():
|
||||||
|
|
||||||
found = 0
|
found = 0
|
||||||
i = 143
|
i = 143
|
||||||
|
|
||||||
@ -31,13 +28,9 @@ def main():
|
|||||||
if is_pentagonal(n):
|
if is_pentagonal(n):
|
||||||
found = 1
|
found = 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 45')
|
print('Project Euler, Problem 45')
|
||||||
print(f'Answer: {n}')
|
print(f'Answer: {n}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p045()
|
||||||
|
@ -13,9 +13,7 @@
|
|||||||
#
|
#
|
||||||
# What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
|
# 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, timing
|
||||||
|
|
||||||
from projecteuler import sieve
|
|
||||||
|
|
||||||
|
|
||||||
N = 10000
|
N = 10000
|
||||||
@ -44,9 +42,9 @@ def goldbach(n):
|
|||||||
# Return 0 if no solution is found.
|
# Return 0 if no solution is found.
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def main():
|
|
||||||
start = default_timer()
|
|
||||||
|
|
||||||
|
@timing
|
||||||
|
def p046():
|
||||||
found = 0
|
found = 0
|
||||||
i = 3
|
i = 3
|
||||||
|
|
||||||
@ -59,13 +57,9 @@ def main():
|
|||||||
found = 1
|
found = 1
|
||||||
i = i + 2
|
i = i + 2
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 46')
|
print('Project Euler, Problem 46')
|
||||||
print(f'Answer: {i - 2}')
|
print(f'Answer: {i - 2}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p046()
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
#
|
#
|
||||||
# Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
|
# 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
|
# Function using a modified sieve of Eratosthenes to count
|
||||||
@ -31,9 +31,8 @@ def count_factors(n):
|
|||||||
return factors
|
return factors
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p047():
|
||||||
|
|
||||||
N = 150000
|
N = 150000
|
||||||
|
|
||||||
factors = count_factors(N)
|
factors = count_factors(N)
|
||||||
@ -52,13 +51,9 @@ def main():
|
|||||||
res = i - 3
|
res = i - 3
|
||||||
break
|
break
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 47')
|
print('Project Euler, Problem 47')
|
||||||
print(f'Answer: {res}')
|
print(f'Answer: {res}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p047()
|
||||||
|
@ -4,12 +4,11 @@
|
|||||||
#
|
#
|
||||||
# Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.
|
# 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():
|
@timing
|
||||||
start = default_timer()
|
def p048():
|
||||||
|
|
||||||
sum_ = 0
|
sum_ = 0
|
||||||
|
|
||||||
# Simply calculate the sum of the powers
|
# Simply calculate the sum of the powers
|
||||||
@ -17,13 +16,9 @@ def main():
|
|||||||
power = i ** i
|
power = i ** i
|
||||||
sum_ = sum_ + power
|
sum_ = sum_ + power
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 48')
|
print('Project Euler, Problem 48')
|
||||||
print(f'Answer: {str(sum_)[-10:]}')
|
print(f'Answer: {str(sum_)[-10:]}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p048()
|
||||||
|
@ -8,14 +8,11 @@
|
|||||||
#
|
#
|
||||||
# What 12-digit number do you form by concatenating the three terms in this sequence?
|
# What 12-digit number do you form by concatenating the three terms in this sequence?
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import sieve, timing
|
||||||
|
|
||||||
from projecteuler import sieve
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p049():
|
||||||
|
|
||||||
N = 10000
|
N = 10000
|
||||||
|
|
||||||
primes = sieve(N)
|
primes = sieve(N)
|
||||||
@ -30,8 +27,7 @@ def main():
|
|||||||
while i < N:
|
while i < N:
|
||||||
if primes[i] == 1:
|
if primes[i] == 1:
|
||||||
for j in range(1, 4255):
|
for j in range(1, 4255):
|
||||||
# If i, i+j and i+2*j are all primes and they have
|
# If i, i+j and i+2*j are all primes and they have all the same digits, the result has been found.
|
||||||
# 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\
|
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+j))) and\
|
||||||
''.join(sorted(str(i))) == ''.join(sorted(str(i+2*j))):
|
''.join(sorted(str(i))) == ''.join(sorted(str(i+2*j))):
|
||||||
@ -42,13 +38,9 @@ def main():
|
|||||||
|
|
||||||
i = i + 2
|
i = i + 2
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 49')
|
print('Project Euler, Problem 49')
|
||||||
print(f'Answer: {str(i)+str(i+j)+str(i+2*j)}')
|
print(f'Answer: {str(i)+str(i+j)+str(i+2*j)}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p049()
|
||||||
|
@ -10,14 +10,11 @@
|
|||||||
#
|
#
|
||||||
# Which prime, below one-million, can be written as the sum of the most consecutive primes?
|
# 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, timing
|
||||||
|
|
||||||
from projecteuler import sieve
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p050():
|
||||||
|
|
||||||
N = 1000000
|
N = 1000000
|
||||||
|
|
||||||
primes = sieve(N)
|
primes = sieve(N)
|
||||||
@ -63,13 +60,9 @@ def main():
|
|||||||
|
|
||||||
j = j + 2
|
j = j + 2
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 50')
|
print('Project Euler, Problem 50')
|
||||||
print(f'Answer: {max_p}')
|
print(f'Answer: {max_p}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p050()
|
||||||
|
@ -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
|
# 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.
|
# value family.
|
||||||
|
|
||||||
from timeit import default_timer
|
from projecteuler import sieve, timing
|
||||||
|
|
||||||
from projecteuler import sieve
|
|
||||||
|
|
||||||
|
|
||||||
N = 1000000
|
N = 1000000
|
||||||
|
|
||||||
# N set to 1000000 as a reasonable limit, which turns out to be enough.
|
# N set to 1000000 as a reasonable limit, which turns out to be enough.
|
||||||
primes = sieve(N)
|
primes = sieve(N)
|
||||||
|
|
||||||
@ -64,9 +61,9 @@ def replace(n):
|
|||||||
|
|
||||||
return max_
|
return max_
|
||||||
|
|
||||||
def main():
|
|
||||||
start = default_timer()
|
|
||||||
|
|
||||||
|
@timing
|
||||||
|
def p051():
|
||||||
# Checking only odd numbers with at least 4 digits.
|
# Checking only odd numbers with at least 4 digits.
|
||||||
i = 1001
|
i = 1001
|
||||||
|
|
||||||
@ -83,13 +80,9 @@ def main():
|
|||||||
break
|
break
|
||||||
i = i + 2
|
i = i + 2
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 51')
|
print('Project Euler, Problem 51')
|
||||||
print(f'Answer: {i}')
|
print(f'Answer: {i}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p051()
|
||||||
|
@ -4,12 +4,11 @@
|
|||||||
#
|
#
|
||||||
# Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
|
# 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():
|
@timing
|
||||||
start = default_timer()
|
def p052():
|
||||||
|
|
||||||
i = 1
|
i = 1
|
||||||
|
|
||||||
# Brute force approach, try every integer number until the desired one is found.
|
# Brute force approach, try every integer number until the desired one is found.
|
||||||
@ -22,13 +21,9 @@ def main():
|
|||||||
break
|
break
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 52')
|
print('Project Euler, Problem 52')
|
||||||
print(f'Answer: {i}')
|
print(f'Answer: {i}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p052()
|
||||||
|
@ -12,14 +12,13 @@
|
|||||||
#
|
#
|
||||||
# How many, not necessarily distinct, values of (n r) for 1≤n≤100, are greater than one-million?
|
# 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 scipy.special import comb
|
||||||
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
def main():
|
|
||||||
start = default_timer()
|
|
||||||
|
|
||||||
|
@timing
|
||||||
|
def p053():
|
||||||
LIMIT = 1000000
|
LIMIT = 1000000
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
@ -30,13 +29,9 @@ def main():
|
|||||||
if comb(i, j) > LIMIT:
|
if comb(i, j) > LIMIT:
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 53')
|
print('Project Euler, Problem 53')
|
||||||
print(f'Answer: {count}')
|
print(f'Answer: {count}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p053()
|
||||||
|
@ -48,7 +48,8 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from timeit import default_timer
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
class Value(IntEnum):
|
class Value(IntEnum):
|
||||||
@ -111,7 +112,7 @@ class Hand():
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.cards = list()
|
self.cards = []
|
||||||
|
|
||||||
def sort(self):
|
def sort(self):
|
||||||
self.cards.sort(key=lambda x: x.value)
|
self.cards.sort(key=lambda x: x.value)
|
||||||
@ -126,7 +127,6 @@ class Game():
|
|||||||
self.hand2 = None
|
self.hand2 = None
|
||||||
|
|
||||||
def play(self):
|
def play(self):
|
||||||
|
|
||||||
# If player 1 has a Royal Flush, player 1 wins.
|
# 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\
|
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\
|
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:
|
if not full1 and full2:
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
# If both players have a full house, check who has the highest value
|
# 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
|
||||||
# for the three equal cards (the third card in the array will be part
|
|
||||||
# of the set of three).
|
# of the set of three).
|
||||||
if full1 and full2:
|
if full1 and full2:
|
||||||
if self.hand1.cards[2].value > self.hand2.cards[2].value:
|
if self.hand1.cards[2].value > self.hand2.cards[2].value:
|
||||||
@ -443,9 +442,8 @@ class Game():
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p054():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open('p054_poker.txt', 'r', encoding='utf-8') as fp:
|
with open('p054_poker.txt', 'r', encoding='utf-8') as fp:
|
||||||
games = fp.readlines()
|
games = fp.readlines()
|
||||||
@ -476,7 +474,7 @@ def main():
|
|||||||
hand1.sort()
|
hand1.sort()
|
||||||
hand2.sort()
|
hand2.sort()
|
||||||
|
|
||||||
for k in hand1.cards:
|
for _ in hand1.cards:
|
||||||
game = Game()
|
game = Game()
|
||||||
game.hand1 = hand1
|
game.hand1 = hand1
|
||||||
game.hand2 = hand2
|
game.hand2 = hand2
|
||||||
@ -484,13 +482,9 @@ def main():
|
|||||||
if game.play() == 1:
|
if game.play() == 1:
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 54')
|
print('Project Euler, Problem 54')
|
||||||
print(f'Answer: {count}')
|
print(f'Answer: {count}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p054()
|
||||||
|
@ -23,9 +23,7 @@
|
|||||||
#
|
#
|
||||||
# NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.
|
# 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, timing
|
||||||
|
|
||||||
from projecteuler import is_palindrome
|
|
||||||
|
|
||||||
|
|
||||||
def is_lychrel(n):
|
def is_lychrel(n):
|
||||||
@ -53,9 +51,8 @@ def is_lychrel(n):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p055():
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
# For each number, use the is_lychrel function to check if the number
|
# For each number, use the is_lychrel function to check if the number
|
||||||
@ -64,13 +61,9 @@ def main():
|
|||||||
if is_lychrel(i):
|
if is_lychrel(i):
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 55')
|
print('Project Euler, Problem 55')
|
||||||
print(f'Answer: {count}')
|
print(f'Answer: {count}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p055()
|
||||||
|
@ -5,12 +5,11 @@
|
|||||||
#
|
#
|
||||||
# Considering natural numbers of the form, a^b, where a, b < 100, what is the maximum digital sum?
|
# 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():
|
@timing
|
||||||
start = default_timer()
|
def p056():
|
||||||
|
|
||||||
max_ = 0
|
max_ = 0
|
||||||
|
|
||||||
# Straightforward brute force approach
|
# Straightforward brute force approach
|
||||||
@ -26,13 +25,9 @@ def main():
|
|||||||
if sum_ > max_:
|
if sum_ > max_:
|
||||||
max_ = sum_
|
max_ = sum_
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 56')
|
print('Project Euler, Problem 56')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {max_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p056()
|
||||||
|
@ -16,12 +16,11 @@
|
|||||||
#
|
#
|
||||||
# In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator?
|
# 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():
|
@timing
|
||||||
start = default_timer()
|
def p057():
|
||||||
|
|
||||||
n = 1
|
n = 1
|
||||||
d = 1
|
d = 1
|
||||||
count = 0
|
count = 0
|
||||||
@ -36,13 +35,9 @@ def main():
|
|||||||
if len(str(n)) > len(str(d)):
|
if len(str(n)) > len(str(d)):
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 57')
|
print('Project Euler, Problem 57')
|
||||||
print(f'Answer: {count}')
|
print(f'Answer: {count}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p057()
|
||||||
|
@ -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,
|
# 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%?
|
# 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, timing
|
||||||
|
|
||||||
from projecteuler import is_prime
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
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)
|
# 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
|
# 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
|
# 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:
|
if ratio < 0.1:
|
||||||
break
|
break
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 58')
|
print('Project Euler, Problem 58')
|
||||||
print(f'Answer: {l}')
|
print(f'Answer: {l}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p058()
|
||||||
|
@ -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
|
# 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.
|
# ASCII values in the original text.
|
||||||
|
|
||||||
from timeit import default_timer
|
import sys
|
||||||
|
|
||||||
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
class EncryptedText():
|
class EncryptedText():
|
||||||
@ -40,6 +42,8 @@ class EncryptedText():
|
|||||||
|
|
||||||
self.len = len(self.text)
|
self.len = len(self.text)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
def decrypt(self):
|
def decrypt(self):
|
||||||
found = 0
|
found = 0
|
||||||
|
|
||||||
@ -77,9 +81,8 @@ class EncryptedText():
|
|||||||
return plain_text
|
return plain_text
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p059():
|
||||||
|
|
||||||
enc_text = EncryptedText()
|
enc_text = EncryptedText()
|
||||||
|
|
||||||
if enc_text.read_text('p059_cipher.txt') == -1:
|
if enc_text.read_text('p059_cipher.txt') == -1:
|
||||||
@ -92,13 +95,9 @@ def main():
|
|||||||
for i in list(plain_text):
|
for i in list(plain_text):
|
||||||
sum_ = sum_ + ord(i)
|
sum_ = sum_ + ord(i)
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 59')
|
print('Project Euler, Problem 59')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p059()
|
||||||
|
@ -6,14 +6,11 @@
|
|||||||
#
|
#
|
||||||
# Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
|
# 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 is_prime, sieve, timing
|
||||||
|
|
||||||
from projecteuler import sieve, is_prime
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
@timing
|
||||||
start = default_timer()
|
def p060():
|
||||||
|
|
||||||
N = 10000
|
N = 10000
|
||||||
|
|
||||||
primes = sieve(N)
|
primes = sieve(N)
|
||||||
@ -85,13 +82,9 @@ def main():
|
|||||||
|
|
||||||
p1 = p1 + 2
|
p1 = p1 + 2
|
||||||
|
|
||||||
end = default_timer()
|
|
||||||
|
|
||||||
print('Project Euler, Problem 60')
|
print('Project Euler, Problem 60')
|
||||||
print(f'Answer: {n}')
|
print(f'Answer: {n}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
p060()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user