From fd46a625d685d271fa0f45d39e204d5e7a0cac9b Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Mon, 17 Aug 2020 17:48:23 +0530 Subject: [PATCH 1/8] Create __init__.py --- project_euler/problem_37/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 project_euler/problem_37/__init__.py diff --git a/project_euler/problem_37/__init__.py b/project_euler/problem_37/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_37/__init__.py @@ -0,0 +1 @@ +# From a5c348baef2f26673aeb4e34db0b026fba8e7002 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Mon, 17 Aug 2020 17:51:24 +0530 Subject: [PATCH 2/8] Add files via upload --- project_euler/problem_37/sol1.py | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 project_euler/problem_37/sol1.py diff --git a/project_euler/problem_37/sol1.py b/project_euler/problem_37/sol1.py new file mode 100644 index 000000000000..e00d4651689c --- /dev/null +++ b/project_euler/problem_37/sol1.py @@ -0,0 +1,89 @@ +""" +The number 3797 has an interesting property. Being prime itself, it is possible +to continuously remove digits from left to right, and remain prime at each stage: +3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. + +Find the sum of the only eleven primes that are both truncatable from left to right +and right to left. + +NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. +""" + + +from typing import List + +seive = [True] * 1000001 +seive[1] = False +i = 2 +while i * i <= 1000000: + if seive[i]: + for j in range(i * i, 1000001, i): + seive[j] = False + i += 1 + + +def is_prime(n: int) -> bool: + """ + Returns True if n is prime, + False otherwise, for 1 <= n <= 1000000 + >>> is_prime(87) + False + >>> is_prime(1) + False + >>> is_prime(25363) + False + """ + return seive[n] + + +def list_truncated_nums(n: int) -> List[int]: + """ + Returns a list of all left and right truncated numbers of n + >>> list_truncated_nums(927628) + [927628, 27628, 92762, 7628, 9276, 628, 927, 28, 92, 8, 9] + >>> list_truncated_nums(467) + [467, 67, 46, 7, 4] + >>> list_truncated_nums(58) + [58, 8, 5] + """ + str_num = str(n) + list_nums = [n] + for i in range(1, len(str_num)): + list_nums.append(int(str_num[i:])) + list_nums.append(int(str_num[:-i])) + return list_nums + + +def validate(n): + """ + To optimize the approach, we will rule out the numbers above 1000, + whose last or first digit is even, or + the last or first two digits are not prime + >>> validate(74679) + False + >>> validate(235693) + False + >>> validate(3797) + True + """ + if len(str(n)) > 3: + if not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3])): + return False + return True + + +def compute_truncated_primes(count: int = 11) -> int: + """ + Returns the sum of truncated primes + >>> compute_truncated_primes(11) + 748317 + """ + list_truncated_primes = [] + num = 13 + while len(list_truncated_primes) != count: + if validate(num): + list_nums = list_truncated_nums(num) + if all(is_prime(i) for i in list_nums): + list_truncated_primes.append(num) + num += 2 + return sum(list_truncated_primes) From 33de04d454242b1b8de82c067dcb79968bb919b8 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Mon, 17 Aug 2020 17:53:38 +0530 Subject: [PATCH 3/8] Update sol1.py --- project_euler/problem_37/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_37/sol1.py b/project_euler/problem_37/sol1.py index e00d4651689c..7fd632877180 100644 --- a/project_euler/problem_37/sol1.py +++ b/project_euler/problem_37/sol1.py @@ -54,7 +54,7 @@ def list_truncated_nums(n: int) -> List[int]: return list_nums -def validate(n): +def validate(n: int) -> bool: """ To optimize the approach, we will rule out the numbers above 1000, whose last or first digit is even, or From 9479a72f9006096bd960985665325df10d9f7ee8 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Mon, 17 Aug 2020 17:57:38 +0530 Subject: [PATCH 4/8] Update sol1.py --- project_euler/problem_37/sol1.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_37/sol1.py b/project_euler/problem_37/sol1.py index 7fd632877180..716162311fd7 100644 --- a/project_euler/problem_37/sol1.py +++ b/project_euler/problem_37/sol1.py @@ -72,9 +72,9 @@ def validate(n: int) -> bool: return True -def compute_truncated_primes(count: int = 11) -> int: +def compute_truncated_primes(count: int = 11) -> List[int]: """ - Returns the sum of truncated primes + Returns the list of truncated primes >>> compute_truncated_primes(11) 748317 """ @@ -86,4 +86,8 @@ def compute_truncated_primes(count: int = 11) -> int: if all(is_prime(i) for i in list_nums): list_truncated_primes.append(num) num += 2 - return sum(list_truncated_primes) + return list_truncated_primes + + +if __name__ == "__main__": + print(f"{sum(compute_truncated_primes(11))}") From e77e3a567f8787cfae67fa813c5bf0b464ec5ffd Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Mon, 17 Aug 2020 17:59:30 +0530 Subject: [PATCH 5/8] Update sol1.py --- project_euler/problem_37/sol1.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/project_euler/problem_37/sol1.py b/project_euler/problem_37/sol1.py index 716162311fd7..c1b174bdb419 100644 --- a/project_euler/problem_37/sol1.py +++ b/project_euler/problem_37/sol1.py @@ -57,8 +57,7 @@ def list_truncated_nums(n: int) -> List[int]: def validate(n: int) -> bool: """ To optimize the approach, we will rule out the numbers above 1000, - whose last or first digit is even, or - the last or first two digits are not prime + whose first or last two digits are not prime >>> validate(74679) False >>> validate(235693) From be356f42495b8dc69104862d5e3240eca76ded57 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Mon, 17 Aug 2020 18:17:00 +0530 Subject: [PATCH 6/8] Update sol1.py --- project_euler/problem_37/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_37/sol1.py b/project_euler/problem_37/sol1.py index c1b174bdb419..cde5ab29f81e 100644 --- a/project_euler/problem_37/sol1.py +++ b/project_euler/problem_37/sol1.py @@ -75,7 +75,7 @@ def compute_truncated_primes(count: int = 11) -> List[int]: """ Returns the list of truncated primes >>> compute_truncated_primes(11) - 748317 + [23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397] """ list_truncated_primes = [] num = 13 From b69b79fe6e6e8f0b167a40914875eb3f865f626a Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Mon, 17 Aug 2020 18:53:08 +0530 Subject: [PATCH 7/8] Update sol1.py --- project_euler/problem_37/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_37/sol1.py b/project_euler/problem_37/sol1.py index cde5ab29f81e..f50ca8202754 100644 --- a/project_euler/problem_37/sol1.py +++ b/project_euler/problem_37/sol1.py @@ -57,7 +57,7 @@ def list_truncated_nums(n: int) -> List[int]: def validate(n: int) -> bool: """ To optimize the approach, we will rule out the numbers above 1000, - whose first or last two digits are not prime + whose first or last three digits are not prime >>> validate(74679) False >>> validate(235693) From 52465d00a2833f7da5dde35bacdacc623d1b80e9 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Mon, 17 Aug 2020 20:01:49 +0530 Subject: [PATCH 8/8] Update project_euler/problem_37/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_37/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_37/sol1.py b/project_euler/problem_37/sol1.py index f50ca8202754..c01d64d83fbe 100644 --- a/project_euler/problem_37/sol1.py +++ b/project_euler/problem_37/sol1.py @@ -89,4 +89,4 @@ def compute_truncated_primes(count: int = 11) -> List[int]: if __name__ == "__main__": - print(f"{sum(compute_truncated_primes(11))}") + print(f"{sum(compute_truncated_primes(11)) = }")