32 lines
606 B
Python
32 lines
606 B
Python
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)
|
|
|