Skip to content

Added solutoin for Project Euler problem 125 #2988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
72 changes: 72 additions & 0 deletions project_euler/problem_125/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
"https://projecteuler.net/problem=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.

"""


import math


def gen_square_sums(limit: int) -> list:
"""
Generates and returns thee square sums till n.
Using the formula 1^2 + 2^2 + .. + n^2 = n * (n + 1) * (2n + 1) / 6
"""
square_sums = []
for n in range(limit):
square_sum = (n * (n + 1) * (2 * n + 1)) // 6
square_sums.append(square_sum)
return square_sums


def gen_palindromic_square_sums(square_sums) -> list:
"""
Filters and returns the palindromic square sums
Difference between any two index gives the sum of squares in that range.
"""
palindromic_square_sums = set()
for i in range(len(square_sums) - 2):
for j in range(i + 2, len(square_sums)):
diff = square_sums[j] - square_sums[i]
if is_palindrome(diff):
palindromic_square_sums.add(diff)
return sorted(palindromic_square_sums)


def is_palindrome(n: int) -> bool:
"""
Returns if the number is palindrome or not
"""
return str(n) == str(n)[::-1]


def solution(limit: int = 1e8) -> int:
"""
Returns the sum of palindromic squares sums till 10^8.
"""
square_sums = gen_square_sums(int(math.sqrt(limit)))
palindromic_square_sums = gen_palindromic_square_sums(square_sums)

res = index = 0
while palindromic_square_sums[index] <= limit:
res += palindromic_square_sums[index]
index += 1
return res


if __name__ == "__main__":
print(solution())