From c64c785ea10416f66bb1cdec32da43aac21abd52 Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Thu, 8 Oct 2020 23:17:55 +0200 Subject: [PATCH 1/2] Added solution for Project Euler problem 125 --- project_euler/problem_125/__init__.py | 0 project_euler/problem_125/sol1.py | 56 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 project_euler/problem_125/__init__.py create mode 100644 project_euler/problem_125/sol1.py diff --git a/project_euler/problem_125/__init__.py b/project_euler/problem_125/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_125/sol1.py b/project_euler/problem_125/sol1.py new file mode 100644 index 000000000000..0f1d06497b37 --- /dev/null +++ b/project_euler/problem_125/sol1.py @@ -0,0 +1,56 @@ +""" +Problem 125: https://projecteuler.net/problem=125 + +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 108 that are both palindromic and can +be written as the sum of consecutive squares. +""" + + +def is_palindrome(n: int) -> bool: + """ + Check if an integer is palindromic. + >>> is_palindrome(12521) + True + >>> is_palindrome(12522) + False + >>> is_palindrome(12210) + False + """ + if n % 10 == 0: + return False + s = str(n) + return s == s[::-1] + + +def solution() -> int: + """ + Retuens the sum of all numbers less than 1e8 that are both palindromic and + can be written as the sum of consecutive squares. + """ + LIMIT = 10 ** 8 + answer = set() + first_square = 1 + sum_squares = 5 + while sum_squares < LIMIT: + last_square = first_square + 1 + while sum_squares < LIMIT: + if is_palindrome(sum_squares): + answer.add(sum_squares) + last_square += 1 + sum_squares += last_square ** 2 + first_square += 1 + sum_squares = first_square ** 2 + (first_square + 1) ** 2 + + return sum(answer) + + +if __name__ == "__main__": + print(solution()) From 0f5f04f8feecbc7b31a01d5d72b038daaf353ca5 Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Fri, 9 Oct 2020 00:19:25 +0200 Subject: [PATCH 2/2] Fixed typos --- project_euler/problem_125/sol1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_125/sol1.py b/project_euler/problem_125/sol1.py index 0f1d06497b37..afc1f2890cef 100644 --- a/project_euler/problem_125/sol1.py +++ b/project_euler/problem_125/sol1.py @@ -9,7 +9,7 @@ 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 108 that are both palindromic and can +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. """ @@ -32,7 +32,7 @@ def is_palindrome(n: int) -> bool: def solution() -> int: """ - Retuens the sum of all numbers less than 1e8 that are both palindromic and + Returns the sum of all numbers less than 1e8 that are both palindromic and can be written as the sum of consecutive squares. """ LIMIT = 10 ** 8