Brute force solve of problem 348

This commit is contained in:
2026-07-18 11:14:20 -04:00
parent ae82bc4418
commit 4a21124844
+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)