-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Hacktoberfest: Add project euler problem 51 #3018
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
dhruvmanila
merged 2 commits into
TheAlgorithms:master
from
Hyftar:project-euler-problem-51
Oct 12, 2020
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Empty file.
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,106 @@ | ||
from collections import Counter | ||
Hyftar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
""" | ||
https://projecteuler.net/problem=51 | ||
Prime digit replacements | ||
Problem 51 | ||
|
||
By replacing the 1st digit of the 2-digit number *3, it turns out that six of | ||
the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime. | ||
|
||
By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit | ||
number is the first example having seven primes among the ten generated numbers, | ||
yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. | ||
Consequently 56003, being the first member of this family, is the smallest prime | ||
with this property. | ||
|
||
Find the smallest prime which, by replacing part of the number (not necessarily | ||
adjacent digits) with the same digit, is part of an eight prime value family. | ||
""" | ||
|
||
|
||
def sieve(n: int) -> list: | ||
Hyftar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Sieve of Erotosthenes | ||
Function to return all the prime numbers up to a certain number | ||
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | ||
|
||
>>> sieve(3) | ||
[2] | ||
|
||
>>> sieve(50) | ||
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] | ||
""" | ||
is_prime = [True] * n | ||
is_prime[0] = False | ||
is_prime[1] = False | ||
is_prime[2] = True | ||
|
||
for i in range(3, int(n ** 0.5 + 1), 2): | ||
index = i * 2 | ||
while index < n: | ||
is_prime[index] = False | ||
index = index + i | ||
|
||
primes = [2] | ||
|
||
for i in range(3, n, 2): | ||
if is_prime[i]: | ||
primes.append(i) | ||
|
||
return primes | ||
|
||
|
||
def digit_replacements(number: int) -> list: | ||
""" | ||
Returns all the possible families of digit replacements in a number which | ||
contains at least one repeating digit | ||
|
||
>>> digit_replacements(544) | ||
[[500, 511, 522, 533, 544, 555, 566, 577, 588, 599]] | ||
|
||
>>> digit_replacements(3112) | ||
[[3002, 3112, 3222, 3332, 3442, 3552, 3662, 3772, 3882, 3992]] | ||
""" | ||
number = str(number) | ||
replacements = [] | ||
digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] | ||
|
||
for duplicate in Counter(number) - Counter(set(number)): | ||
family = [int(number.replace(duplicate, digit)) for digit in digits] | ||
replacements.append(family) | ||
|
||
return replacements | ||
|
||
|
||
def solution() -> int: | ||
""" | ||
Returns the solution of the problem | ||
|
||
>>> solution() | ||
121313 | ||
Hyftar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
numbers_checked = set() | ||
primes = set(sieve(1_000_000)) | ||
|
||
# Filter primes with less than 3 replaceable digits | ||
primes = {x for x in primes if len(str(x)) - len(set(str(x))) >= 3} | ||
Hyftar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for prime in primes: | ||
if prime in numbers_checked: | ||
continue | ||
|
||
replacements = digit_replacements(prime) | ||
|
||
for family in replacements: | ||
numbers_checked.update(family) | ||
primes_in_family = primes.intersection(family) | ||
|
||
if len(primes_in_family) != 8: | ||
continue | ||
|
||
return min(primes_in_family) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(solution()) |
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.