From 8024e694019ed41f3119934b3dad1897f95f9a0a Mon Sep 17 00:00:00 2001 From: nstoik Date: Thu, 8 Oct 2020 09:45:30 -0600 Subject: [PATCH 1/6] add problem url. Add typehint, default value and doctest --- project_euler/problem_36/sol1.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_36/sol1.py b/project_euler/problem_36/sol1.py index 39088cf25dd4..205568b07eeb 100644 --- a/project_euler/problem_36/sol1.py +++ b/project_euler/problem_36/sol1.py @@ -1,4 +1,9 @@ """ +Project Euler Problem 36 +https://projecteuler.net/problem=36 + +Problem Statement: + Double-base palindromes Problem 36 The decimal number, 585 = 10010010012 (binary), is palindromic in both bases. @@ -10,8 +15,23 @@ leading zeros.) """ +from typing import Union + -def is_palindrome(n): +def is_palindrome(n: Union[int, str]) -> bool: + """ + Return true if the input n is a palindrome. + Otherwise return false. n can be an integer or a string. + + >>> is_palindrome(909) + True + >>> is_palindrome(908) + False + >>> is_palindrome('10101') + True + >>> is_palindrome('10111') + False + """ n = str(n) if n == n[::-1]: @@ -20,7 +40,7 @@ def is_palindrome(n): return False -def solution(n): +def solution(n: int = 1000000): """Return the sum of all numbers, less than n , which are palindromic in base 10 and base 2. From ac27b3879ae23b65bdc5b177a5f72c81a38acd92 Mon Sep 17 00:00:00 2001 From: nstoik Date: Thu, 8 Oct 2020 09:46:01 -0600 Subject: [PATCH 2/6] run black --- project_euler/problem_36/sol1.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_36/sol1.py b/project_euler/problem_36/sol1.py index 205568b07eeb..926d15ea9bf8 100644 --- a/project_euler/problem_36/sol1.py +++ b/project_euler/problem_36/sol1.py @@ -23,13 +23,13 @@ def is_palindrome(n: Union[int, str]) -> bool: Return true if the input n is a palindrome. Otherwise return false. n can be an integer or a string. - >>> is_palindrome(909) + >>> is_palindrome(909) True - >>> is_palindrome(908) + >>> is_palindrome(908) False - >>> is_palindrome('10101') + >>> is_palindrome('10101') True - >>> is_palindrome('10111') + >>> is_palindrome('10111') False """ n = str(n) From 74997b6a8b0a35ea447ad2bd3703f054fa5a0b05 Mon Sep 17 00:00:00 2001 From: nstoik Date: Thu, 8 Oct 2020 10:09:32 -0600 Subject: [PATCH 3/6] add project url. add solution function for problem 35 --- project_euler/problem_35/sol1.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 5f023c56ae50..7ab74ce0b573 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -1,4 +1,9 @@ """ +Project Euler Problem 35 +https://projecteuler.net/problem=35 + +Problem Statement: + The number 197 is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, @@ -11,6 +16,7 @@ generate each circular combination of the number and check if all are prime. """ from __future__ import annotations +from typing import List seive = [True] * 1000001 i = 2 @@ -47,7 +53,7 @@ def contains_an_even_digit(n: int) -> bool: return any(digit in "02468" for digit in str(n)) -def find_circular_primes(limit: int = 1000000) -> list[int]: +def find_circular_primes(limit: int = 1000000) -> List[int]: """ Return circular primes below limit. >>> len(find_circular_primes(100)) @@ -65,5 +71,13 @@ def find_circular_primes(limit: int = 1000000) -> list[int]: return result +def solution() -> int: + """ + >>> solution() + 55 + """ + return len(find_circular_primes()) + + if __name__ == "__main__": print(f"{len(find_circular_primes()) = }") From 3720b01203cb6fc34f880d3f1cfd9ef56d89baf7 Mon Sep 17 00:00:00 2001 From: nstoik Date: Thu, 8 Oct 2020 10:13:43 -0600 Subject: [PATCH 4/6] add space between imports on problem 35 --- project_euler/problem_35/sol1.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 7ab74ce0b573..0c101d440c91 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -16,6 +16,7 @@ generate each circular combination of the number and check if all are prime. """ from __future__ import annotations + from typing import List seive = [True] * 1000001 From 43d76d38afb4276048675f9e8cb377f98dcaeb3a Mon Sep 17 00:00:00 2001 From: Dhruv Date: Fri, 9 Oct 2020 08:27:08 +0530 Subject: [PATCH 5/6] Update sol1.py --- project_euler/problem_35/sol1.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 0c101d440c91..17a4e9088ae2 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -17,8 +17,6 @@ """ from __future__ import annotations -from typing import List - seive = [True] * 1000001 i = 2 while i * i <= 1000000: @@ -54,7 +52,7 @@ def contains_an_even_digit(n: int) -> bool: return any(digit in "02468" for digit in str(n)) -def find_circular_primes(limit: int = 1000000) -> List[int]: +def find_circular_primes(limit: int = 1000000) -> list[int]: """ Return circular primes below limit. >>> len(find_circular_primes(100)) From 8cec47f64ec0be8cf5f7b67ee08100550f44636e Mon Sep 17 00:00:00 2001 From: Dhruv Date: Fri, 9 Oct 2020 08:28:10 +0530 Subject: [PATCH 6/6] Update sol1.py --- project_euler/problem_36/sol1.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/project_euler/problem_36/sol1.py b/project_euler/problem_36/sol1.py index 926d15ea9bf8..13a749862e5f 100644 --- a/project_euler/problem_36/sol1.py +++ b/project_euler/problem_36/sol1.py @@ -33,11 +33,7 @@ def is_palindrome(n: Union[int, str]) -> bool: False """ n = str(n) - - if n == n[::-1]: - return True - else: - return False + return True if n == n[::-1] else False def solution(n: int = 1000000):