Compare commits
13 Commits
b485914e01
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| a1fa273624 | |||
| cf417eaca2 | |||
| 605c9c7f7f | |||
| e6ed590b73 | |||
| 233fc85708 | |||
| 2fb728a4ba | |||
| e900cb117e | |||
| a036c30fc4 | |||
| 8c3da33225 | |||
| 24fb140672 | |||
| 2df02a8204 | |||
| 8580160d67 | |||
| 5cee1f7998 |
@@ -22,7 +22,7 @@ def create_problem():
|
|||||||
https://projecteuler.net/problem={args["problem"]}
|
https://projecteuler.net/problem={args["problem"]}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from utils import timeit
|
from project_euler_python.utils import timeit
|
||||||
|
|
||||||
|
|
||||||
@timeit("Problem {(args["problem"]):0>3}")
|
@timeit("Problem {(args["problem"]):0>3}")
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ Solution for problem 008 of Project Euler
|
|||||||
https://projecteuler.net/problem=8
|
https://projecteuler.net/problem=8
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import collections
|
from collections import deque
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
|
from math import prod
|
||||||
|
|
||||||
from project_euler_python.utils import timeit
|
from project_euler_python.utils import timeit
|
||||||
|
|
||||||
@@ -19,7 +20,7 @@ from project_euler_python.utils import timeit
|
|||||||
def sliding_window(iterable, n):
|
def sliding_window(iterable, n):
|
||||||
# sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
|
# sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
|
||||||
it = iter(iterable)
|
it = iter(iterable)
|
||||||
window = collections.deque(islice(it, n), maxlen=n)
|
window = deque(islice(it, n), maxlen=n)
|
||||||
if len(window) == n:
|
if len(window) == n:
|
||||||
yield tuple(window)
|
yield tuple(window)
|
||||||
for x in it:
|
for x in it:
|
||||||
@@ -65,12 +66,10 @@ def compute():
|
|||||||
num = NUM.replace("\n", "").replace(" ", "")
|
num = NUM.replace("\n", "").replace(" ", "")
|
||||||
adjacent_digits = 13
|
adjacent_digits = 13
|
||||||
ans = 0
|
ans = 0
|
||||||
for nums in sliding_window(num, adjacent_digits):
|
|
||||||
prod = 1
|
for digits in sliding_window(num, adjacent_digits):
|
||||||
for num in nums:
|
window_product = prod(int(digit) for digit in digits)
|
||||||
prod *= int(num)
|
ans = max(ans, window_product)
|
||||||
if prod > ans:
|
|
||||||
ans = prod
|
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ def compute():
|
|||||||
Find the product abc.
|
Find the product abc.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
upper_limit = 1000
|
upper_limit = 1000 + 1
|
||||||
for a in range(1, upper_limit + 1):
|
for a in range(1, upper_limit):
|
||||||
for b in range(a + 1, upper_limit + 1):
|
for b in range(a + 1, upper_limit):
|
||||||
c = upper_limit - a - b
|
c = upper_limit - a - b
|
||||||
if a * a + b * b == c * c:
|
if a * a + b * b == c * c:
|
||||||
# It is now implied that b < c, because we have a > 0
|
# It is now implied that b < c, because we have a > 0
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ Solution for problem 012 of Project Euler
|
|||||||
https://projecteuler.net/problem=12
|
https://projecteuler.net/problem=12
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from itertools import count
|
from itertools import accumulate, count
|
||||||
from math import floor, sqrt
|
from math import floor, sqrt
|
||||||
|
|
||||||
from project_euler_python.utils import timeit
|
from project_euler_python.utils import timeit
|
||||||
@@ -53,12 +53,10 @@ def compute():
|
|||||||
divisors?
|
divisors?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
triangle = 0
|
limit = 500
|
||||||
for i in count(1):
|
for triangle in accumulate(count(1)):
|
||||||
# This is the ith triangle number, i.e. num = 1 + 2 + ... + i =
|
# This is the ith triangle number, i.e. num = 1 + 2 + ... + i = i*(i+1)/2
|
||||||
# = i*(i+1)/2
|
if num_divisors(triangle) > limit:
|
||||||
triangle += i
|
|
||||||
if num_divisors(triangle) > 500:
|
|
||||||
return str(triangle)
|
return str(triangle)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,14 +12,14 @@ https://projecteuler.net/problem=14
|
|||||||
from project_euler_python.utils import timeit
|
from project_euler_python.utils import timeit
|
||||||
|
|
||||||
|
|
||||||
def chain_length(n, terms):
|
def chain_length(n: int, terms: dict[int, int]) -> int:
|
||||||
length = 0
|
length = 0
|
||||||
while n != 1:
|
while n != 1:
|
||||||
if n in terms:
|
if n in terms:
|
||||||
length += terms[n]
|
length += terms[n]
|
||||||
break
|
break
|
||||||
if n % 2 == 0:
|
if n % 2 == 0:
|
||||||
n = n / 2
|
n //= 2
|
||||||
else:
|
else:
|
||||||
n = 3 * n + 1
|
n = 3 * n + 1
|
||||||
length += 1
|
length += 1
|
||||||
@@ -27,7 +27,7 @@ def chain_length(n, terms):
|
|||||||
|
|
||||||
|
|
||||||
@timeit("Problem 014")
|
@timeit("Problem 014")
|
||||||
def compute():
|
def compute() -> int:
|
||||||
"""
|
"""
|
||||||
The following iterative sequence is defined for the set of positive
|
The following iterative sequence is defined for the set of positive
|
||||||
integers:
|
integers:
|
||||||
@@ -49,15 +49,17 @@ def compute():
|
|||||||
NOTE: Once the chain starts the terms are allowed to go above one million.
|
NOTE: Once the chain starts the terms are allowed to go above one million.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
ans = 0
|
|
||||||
limit = 1_000_000
|
limit = 1_000_000
|
||||||
score = 0
|
ans = 0
|
||||||
terms = dict()
|
longest_length = 0
|
||||||
for i in range(1, limit):
|
chain_lengths = {1: 0}
|
||||||
terms[i] = chain_length(i, terms)
|
|
||||||
if terms[i] > score:
|
for start in range(2, limit):
|
||||||
score = terms[i]
|
current_length = chain_length(start, chain_lengths)
|
||||||
ans = i
|
chain_lengths[start] = current_length
|
||||||
|
if current_length > longest_length:
|
||||||
|
ans = start
|
||||||
|
longest_length = current_length
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ def compute():
|
|||||||
names = sorted(f.read().replace('"', "").split(","))
|
names = sorted(f.read().replace('"', "").split(","))
|
||||||
|
|
||||||
ans = 0
|
ans = 0
|
||||||
for idx, name in enumerate(names, 1):
|
for idx, name in enumerate(names, start=1):
|
||||||
ans += sum(ord(char) - 64 for char in name) * idx
|
ans += sum(ord(char) - 64 for char in name) * idx
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|||||||
@@ -9,9 +9,21 @@ Solution for problem 023 of Project Euler
|
|||||||
https://projecteuler.net/problem=23
|
https://projecteuler.net/problem=23
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from itertools import combinations_with_replacement
|
||||||
|
|
||||||
from project_euler_python.utils import timeit
|
from project_euler_python.utils import timeit
|
||||||
|
|
||||||
|
|
||||||
|
def _get_abundant_numbers(limit):
|
||||||
|
divisor_sums = [0 for _ in range(limit)]
|
||||||
|
|
||||||
|
for divisor in range(1, limit):
|
||||||
|
for multiple in range(divisor * 2, limit, divisor):
|
||||||
|
divisor_sums[multiple] += divisor
|
||||||
|
|
||||||
|
return [number for number in range(1, limit) if divisor_sums[number] > number]
|
||||||
|
|
||||||
|
|
||||||
@timeit("Problem 023")
|
@timeit("Problem 023")
|
||||||
def compute():
|
def compute():
|
||||||
"""
|
"""
|
||||||
@@ -35,25 +47,19 @@ def compute():
|
|||||||
sum of two abundant numbers.
|
sum of two abundant numbers.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
limit = 28124
|
limit = 28123 + 1
|
||||||
divisor_sum = [0] * limit
|
abundant_numbers = _get_abundant_numbers(limit)
|
||||||
for i in range(1, limit):
|
|
||||||
for j in range(i * 2, limit, i):
|
|
||||||
divisor_sum[j] += i
|
|
||||||
|
|
||||||
abundant_nums = [i for (i, x) in enumerate(divisor_sum) if x > i]
|
expressible_sums = {
|
||||||
|
first + second
|
||||||
|
for first, second in combinations_with_replacement(
|
||||||
|
abundant_numbers,
|
||||||
|
2,
|
||||||
|
)
|
||||||
|
if first + second < limit
|
||||||
|
}
|
||||||
|
|
||||||
expressible = [False] * limit
|
return sum(number for number in range(1, limit) if number not in expressible_sums)
|
||||||
for i in abundant_nums:
|
|
||||||
for j in abundant_nums:
|
|
||||||
if i + j < limit:
|
|
||||||
expressible[i + j] = True
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
ans = sum(i for (i, x) in enumerate(expressible) if not x)
|
|
||||||
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ Solution for problem 024 of Project Euler
|
|||||||
https://projecteuler.net/problem=24
|
https://projecteuler.net/problem=24
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from itertools import permutations
|
from itertools import islice, permutations
|
||||||
|
|
||||||
from project_euler_python.utils import timeit
|
from project_euler_python.utils import timeit
|
||||||
|
|
||||||
@@ -28,9 +28,10 @@ def compute():
|
|||||||
0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
limit = 1_000_000
|
||||||
digits = list(range(10))
|
digits = list(range(10))
|
||||||
_permutations = list(permutations(digits))
|
millionth_permutation = next(islice(permutations(digits), limit - 1, limit))
|
||||||
ans = "".join(str(digit) for digit in _permutations[999_999])
|
ans = "".join(str(digit) for digit in millionth_permutation)
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|||||||
@@ -37,11 +37,17 @@ def compute():
|
|||||||
|
|
||||||
cycle_length = 0
|
cycle_length = 0
|
||||||
ans = 0
|
ans = 0
|
||||||
|
# Because denominators with factor 2 contribute only to the terminating
|
||||||
|
# part of a decimal. The repeating behavior comes from the part of the
|
||||||
|
# denominator that is coprime with 10, so evens are skipped
|
||||||
for number in range(3, 1000, 2):
|
for number in range(3, 1000, 2):
|
||||||
if number % 5 == 0:
|
if number % 5 == 0:
|
||||||
continue
|
continue
|
||||||
p = 1
|
p = 1
|
||||||
while 10**p % number != 1:
|
remainder = 10 % number
|
||||||
|
while remainder != 1:
|
||||||
|
# avoid huge exponentiation
|
||||||
|
remainder = (remainder * 10) % number
|
||||||
p += 1
|
p += 1
|
||||||
if p > cycle_length:
|
if p > cycle_length:
|
||||||
cycle_length, ans = p, number
|
cycle_length, ans = p, number
|
||||||
|
|||||||
@@ -29,11 +29,10 @@ def compute():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
ans = set()
|
ans = set()
|
||||||
pandigital = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
|
pandigital = [str(number) for number in range(1, 10)]
|
||||||
|
|
||||||
for x in range(1, 100):
|
for x in range(1, 100):
|
||||||
for y in range(100, 10_000):
|
for y in range(100, 10_000):
|
||||||
# product = x * y
|
|
||||||
if sorted(str(x) + str(y) + str(x * y)) == pandigital:
|
if sorted(str(x) + str(y) + str(x * y)) == pandigital:
|
||||||
ans.add(x * y)
|
ans.add(x * y)
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,7 @@ Solution for problem 036 of Project Euler
|
|||||||
https://projecteuler.net/problem=36
|
https://projecteuler.net/problem=36
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from project_euler_python.utils import timeit
|
from project_euler_python.utils import is_palindrome, timeit
|
||||||
|
|
||||||
|
|
||||||
def is_palidrome(num):
|
|
||||||
return str(num) == str(num)[::-1]
|
|
||||||
|
|
||||||
|
|
||||||
@timeit("Problem 036")
|
@timeit("Problem 036")
|
||||||
@@ -28,7 +24,7 @@ def compute():
|
|||||||
|
|
||||||
ans = 0
|
ans = 0
|
||||||
for i in range(1, 1_000_001, 2):
|
for i in range(1, 1_000_001, 2):
|
||||||
if is_palidrome(i) and is_palidrome(bin(i)[2:]):
|
if is_palindrome(i) and is_palindrome(bin(i)[2:]):
|
||||||
ans += i
|
ans += i
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|||||||
@@ -12,14 +12,6 @@ https://projecteuler.net/problem=41
|
|||||||
from project_euler_python.utils import is_prime, timeit
|
from project_euler_python.utils import is_prime, timeit
|
||||||
|
|
||||||
|
|
||||||
def is_pandigital(number):
|
|
||||||
number = sorted(str(number))
|
|
||||||
check = [str(i) for i in range(1, len(number) + 1)]
|
|
||||||
if number == check:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
@timeit("Problem 041")
|
@timeit("Problem 041")
|
||||||
def compute():
|
def compute():
|
||||||
"""
|
"""
|
||||||
@@ -30,8 +22,10 @@ def compute():
|
|||||||
What is the largest n-digit pandigital prime that exists?
|
What is the largest n-digit pandigital prime that exists?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
pandigital = [str(number) for number in range(1, 10)]
|
||||||
|
|
||||||
for ans in range(7654321, 1, -1):
|
for ans in range(7654321, 1, -1):
|
||||||
if is_pandigital(ans):
|
if sorted(str(ans)) == pandigital[: len(str(ans))]:
|
||||||
if is_prime(ans):
|
if is_prime(ans):
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ def compute():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
ans = []
|
ans = []
|
||||||
pandigital = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
|
pandigital = [str(number) for number in range(1, 10)]
|
||||||
|
|
||||||
for n in permutations(pandigital):
|
for n in permutations(pandigital):
|
||||||
n_ = "".join(n)
|
n_ = "".join(n)
|
||||||
|
|||||||
Reference in New Issue
Block a user