Compare commits

...

4 Commits

Author SHA1 Message Date
24fb140672 Refactor problem 2026-04-06 16:01:42 +02:00
2df02a8204 Refactor problem 2026-04-06 15:49:51 +02:00
8580160d67 Cosmetic change 2026-04-06 15:38:39 +02:00
5cee1f7998 Refactor problem 2026-04-06 15:35:20 +02:00
4 changed files with 28 additions and 29 deletions

View File

@@ -9,8 +9,9 @@ Solution for problem 008 of Project Euler
https://projecteuler.net/problem=8
"""
import collections
from collections import deque
from itertools import islice
from math import prod
from project_euler_python.utils import timeit
@@ -19,7 +20,7 @@ from project_euler_python.utils import timeit
def sliding_window(iterable, n):
# sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
it = iter(iterable)
window = collections.deque(islice(it, n), maxlen=n)
window = deque(islice(it, n), maxlen=n)
if len(window) == n:
yield tuple(window)
for x in it:
@@ -65,12 +66,10 @@ def compute():
num = NUM.replace("\n", "").replace(" ", "")
adjacent_digits = 13
ans = 0
for nums in sliding_window(num, adjacent_digits):
prod = 1
for num in nums:
prod *= int(num)
if prod > ans:
ans = prod
for digits in sliding_window(num, adjacent_digits):
window_product = prod(int(digit) for digit in digits)
ans = max(ans, window_product)
return ans

View File

@@ -24,9 +24,9 @@ def compute():
Find the product abc.
"""
upper_limit = 1000
for a in range(1, upper_limit + 1):
for b in range(a + 1, upper_limit + 1):
upper_limit = 1000 + 1
for a in range(1, upper_limit):
for b in range(a + 1, upper_limit):
c = upper_limit - a - b
if a * a + b * b == c * c:
# It is now implied that b < c, because we have a > 0

View File

@@ -9,7 +9,7 @@ Solution for problem 012 of Project Euler
https://projecteuler.net/problem=12
"""
from itertools import count
from itertools import accumulate, count
from math import floor, sqrt
from project_euler_python.utils import timeit
@@ -53,12 +53,10 @@ def compute():
divisors?
"""
triangle = 0
for i in count(1):
# This is the ith triangle number, i.e. num = 1 + 2 + ... + i =
# = i*(i+1)/2
triangle += i
if num_divisors(triangle) > 500:
limit = 500
for triangle in accumulate(count(1)):
# This is the ith triangle number, i.e. num = 1 + 2 + ... + i = i*(i+1)/2
if num_divisors(triangle) > limit:
return str(triangle)

View File

@@ -12,14 +12,14 @@ https://projecteuler.net/problem=14
from project_euler_python.utils import timeit
def chain_length(n, terms):
def chain_length(n: int, terms: dict[int, int]) -> int:
length = 0
while n != 1:
if n in terms:
length += terms[n]
break
if n % 2 == 0:
n = n / 2
n //= 2
else:
n = 3 * n + 1
length += 1
@@ -27,7 +27,7 @@ def chain_length(n, terms):
@timeit("Problem 014")
def compute():
def compute() -> int:
"""
The following iterative sequence is defined for the set of positive
integers:
@@ -49,15 +49,17 @@ def compute():
NOTE: Once the chain starts the terms are allowed to go above one million.
"""
ans = 0
limit = 1_000_000
score = 0
terms = dict()
for i in range(1, limit):
terms[i] = chain_length(i, terms)
if terms[i] > score:
score = terms[i]
ans = i
ans = 0
longest_length = 0
chain_lengths = {1: 0}
for start in range(2, limit):
current_length = chain_length(start, chain_lengths)
chain_lengths[start] = current_length
if current_length > longest_length:
ans = start
longest_length = current_length
return ans