Skip to content

Commit a944404

Browse files
author
Ravi Kandasamy Sundaram
committed
problem_125: project euler solution 125
Name: Palindromic sums The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2. There are exactly eleven palindromes below one-thousand that can be written as consecutive square sums, and the sum of these palindromes is 4164. Note that 1 = 0^2 + 1^2 has not been included as this problem is concerned with the squares of positive integers. Find the sum of all the numbers less than 10^8 that are both palindromic and can be written as the sum of consecutive squares. Reference: https://projecteuler.net/problem=125 Fixes: #2695
1 parent c9500dc commit a944404

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Diff for: project_euler/problem_125/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_125/sol1.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
"https://projecteuler.net/problem=125"
3+
4+
Name: Palindromic sums
5+
6+
7+
The palindromic number 595 is interesting because it can be written as
8+
the sum of consecutive squares: 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2.
9+
There are exactly eleven palindromes below one-thousand that can be written as
10+
consecutive square sums, and the sum of these palindromes is 4164.
11+
12+
Note that 1 = 0^2 + 1^2 has not been included as this problem is concerned with
13+
the squares of positive integers.
14+
15+
Find the sum of all the numbers less than 10^8
16+
that are both palindromic and can be written as the sum of consecutive squares.
17+
18+
"""
19+
20+
21+
import math
22+
23+
24+
def gen_square_sums(limit: int) -> list:
25+
"""
26+
Generates and returns thee square sums till n.
27+
Using the formula 1^2 + 2^2 + .. + n^2 = n * (n + 1) * (2n + 1) / 6
28+
"""
29+
square_sums = []
30+
for n in range(limit):
31+
square_sum = (n * (n + 1) * (2 * n + 1)) // 6
32+
square_sums.append(square_sum)
33+
return square_sums
34+
35+
36+
def gen_palindromic_square_sums(square_sums) -> list:
37+
"""
38+
Filters and returns the palindromic square sums
39+
Difference between any two index gives the sum of squares in that range.
40+
"""
41+
palindromic_square_sums = set()
42+
for i in range(len(square_sums) - 2):
43+
for j in range(i + 2, len(square_sums)):
44+
diff = square_sums[j] - square_sums[i]
45+
if is_palindrome(diff):
46+
palindromic_square_sums.add(diff)
47+
return sorted(palindromic_square_sums)
48+
49+
50+
def is_palindrome(n: int) -> bool:
51+
"""
52+
Returns if the number is palindrome or not
53+
"""
54+
return str(n) == str(n)[::-1]
55+
56+
57+
def solution(limit: int = 1e8) -> int:
58+
"""
59+
Returns the sum of palindromic squares sums till 10^8.
60+
"""
61+
square_sums = gen_square_sums(int(math.sqrt(limit)))
62+
palindromic_square_sums = gen_palindromic_square_sums(square_sums)
63+
64+
res = index = 0
65+
while palindromic_square_sums[index] <= limit:
66+
res += palindromic_square_sums[index]
67+
index += 1
68+
return res
69+
70+
71+
if __name__ == "__main__":
72+
print(solution())

0 commit comments

Comments
 (0)