-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125.py
More file actions
31 lines (18 loc) · 810 Bytes
/
125.py
File metadata and controls
31 lines (18 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Project Euler Problem 125: https://projecteuler.net/problem=125"""
# Recursively accumulates sums of consecutive squares and totals the palindromic values found.
is_palindrome = lambda n: str(n) == str(n)[::-1]
visited = set()
def recurse(start, num, total, limit):
"""Accumulate palindromic sums of consecutive squares starting at start."""
if total >= limit:
return 0
if is_palindrome(total) and num - start > 0:
if total not in visited:
visited.add(total)
return recurse(start, num + 1, total + (num + 1) * (num + 1), limit)
def solve(limit):
"""Sum all palindromic numbers below limit expressible as sums of consecutive squares."""
for i in range(1, limit):
recurse(i, i, i * i, limit)
return sum(visited)
print(solve(10**8))