-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Created problem_37 in project_euler #2323
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fd46a62
Create __init__.py
Kush1101 a5c348b
Add files via upload
Kush1101 33de04d
Update sol1.py
Kush1101 9479a72
Update sol1.py
Kush1101 e77e3a5
Update sol1.py
Kush1101 be356f4
Update sol1.py
Kush1101 b69b79f
Update sol1.py
Kush1101 52465d0
Update project_euler/problem_37/sol1.py
Kush1101 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
""" | ||
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: int) -> bool: | ||
""" | ||
To optimize the approach, we will rule out the numbers above 1000, | ||
whose first or last three 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) -> List[int]: | ||
""" | ||
Returns the list of truncated primes | ||
>>> compute_truncated_primes(11) | ||
[23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397] | ||
""" | ||
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 list_truncated_primes | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{sum(compute_truncated_primes(11))}") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.