Compare commits

...
3 Commits
Author SHA1 Message Date
felixm 4a21124844 Brute force solve of problem 348 2026-07-18 11:14:20 -04:00
felixm ae82bc4418 Solve problem 231 2026-07-18 08:44:42 -04:00
felixm 7905b68430 Solve problem 710 2026-07-04 12:22:22 -04:00
3 changed files with 151 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
from lib_prime import prime_factors_count
from collections import defaultdict
def sum_factors(n: int, k: int) -> int:
factors_num = defaultdict(int)
for i in range(n - k + 1, n + 1):
for factor, count in prime_factors_count(i).items():
factors_num[factor] += count
for i in range(2, k + 1):
for factor, count in prime_factors_count(i).items():
factors_num[factor] -= count
return sum(factor * count for factor, count in factors_num.items())
def euler_231():
assert sum_factors(10, 3) == 14
return sum_factors(20 * 10**6, 15 * 10**6)
if __name__ == "__main__":
solution = euler_231()
print(f"e231.py: {solution}")
# assert(solution == 0)
+31
View File
@@ -0,0 +1,31 @@
from functools import cache
from collections import defaultdict
@cache
def is_palindrome(n: int) -> bool:
ns = str(n)
return ns == ns[::-1]
def euler_348():
counts = defaultdict(int)
for a in range(30000):
for b in range(3000):
x = a**2 + b**3
if is_palindrome(x):
counts[x] += 1
rs = []
for k, v in counts.items():
if v == 4:
rs.append(k)
assert len(rs) == 5
return sum(rs)
if __name__ == "__main__":
solution = euler_348()
print(f"e348.py: {solution}")
assert(solution == 1004195061)
+95
View File
@@ -0,0 +1,95 @@
MOD = 10**7
SPACE = 10**7
CACHE = [None for _ in range(SPACE * 2)]
CACHE_TILL = [None for _ in range(SPACE)]
CACHE_TILL_IS_TWO_EVEN = [None for _ in range(SPACE)]
CACHE_TILL_IS_TWO_ODD = [None for _ in range(SPACE)]
CACHE_T_FROM_ACC = [None for _ in range(SPACE * 2)]
def t_from_acc(rem: int, is_two: bool) -> int:
offset = rem + SPACE * is_two
if (value := CACHE_T_FROM_ACC[offset]) is not None:
return value
if rem < 0:
return 0
s = t_from(rem, is_two) + t_from_acc(rem - 2, is_two)
s %= MOD
CACHE_T_FROM_ACC[offset] = s
return s
def t_from(rem: int, is_two: bool) -> int:
offset = rem + SPACE * is_two
if (value := CACHE[offset]) is not None:
return value
s = 0
if rem < 0:
s = 0
elif rem == 1:
s = 0
elif rem == 0 and is_two:
s = 1
else:
s += t_from(rem - 2, is_two) # d = 1
s %= MOD
s += t_from(rem - 4, True) # d = 2
s %= MOD
s += t_from_acc(rem - 6, is_two)
s %= MOD
# Naive (too slow):
# for d in range(1, rem // 2 + 1):
# s += t_from(rem - 2 * d, is_two or d == 2)
# s %= MOD
CACHE[offset] = s % MOD
return s % MOD
def t_old(n: int) -> int:
# print(f"t({n}) = ", end="")
s = 0
for i in range(0, n + 1):
rem = n - i
s += t_from(rem, i == 2)
return s % MOD
def t(n: int) -> int:
s = CACHE_TILL[n - 4]
assert s is not None
s += t_from(n - 3, False)
CACHE_TILL[n - 3] = s % MOD
s += t_from(n - 2, True)
s %= MOD
s += t_from(n - 1, False)
s %= MOD
s += t_from(n, False)
s %= MOD
return s
def euler_710():
for i in range(4):
CACHE_TILL[i] = 0
for n in range(4, 50):
t(n)
assert t(5) == 1
assert t(6) == 4
assert t(20) == 824
assert t(42) == 1999923
for n in range(10, SPACE):
tn = t(n)
if tn % 10**6 == 0:
# print(f"t({n}) = {tn} <<<")
return n
# if tn % 10000 == 0:
# print(f"t({n}) = {tn}")
assert False, "Solution not found"
if __name__ == "__main__":
solution = euler_710()
print(f"e710.py: {solution}")
assert solution == 1275000