-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Hacktoberfest: Add a solution for Project Euler 49 #2702
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0da5cea
added doctests in modular_exponential.py
Iqrar99 e658085
Merge branch 'master' of https://github.com/Iqrar99/Python
Iqrar99 4183239
added doctests in modular_exponential.py
Iqrar99 3856fa0
added URL link
Iqrar99 82aed44
Merge branch 'master' of https://github.com/TheAlgorithms/Python
Iqrar99 d3a4913
Merge branch 'master' of https://github.com/TheAlgorithms/Python
Iqrar99 b4e33ab
Merge branch 'master' of https://github.com/TheAlgorithms/Python
Iqrar99 133838c
Merge branch 'master' of https://github.com/TheAlgorithms/Python
Iqrar99 56990e6
updating DIRECTORY.md
f13bba5
Add problem 49 solution
Iqrar99 723f832
Merge branch 'pe-49' of https://github.com/Iqrar99/Python into pe-49
Iqrar99 b92d14f
updating DIRECTORY.md
c748482
Fix several mistakes
Iqrar99 27abe3f
Merge branch 'pe-49' of https://github.com/Iqrar99/Python into pe-49
Iqrar99 771c736
Move the import statements lower
Iqrar99 05c9a36
Update project_euler/problem_49/sol1.py
Iqrar99 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
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,139 @@ | ||
""" | ||
Prime permutations | ||
|
||
Problem 49 | ||
|
||
The arithmetic sequence, 1487, 4817, 8147, in which each of | ||
the terms increases by 3330, is unusual in two ways: | ||
(i) each of the three terms are prime, | ||
(ii) each of the 4-digit numbers are permutations of one another. | ||
|
||
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, | ||
exhibiting this property, but there is one other 4-digit increasing sequence. | ||
|
||
What 12-digit number do you form by concatenating the three terms in this sequence? | ||
|
||
Solution: | ||
|
||
First, we need to generate all 4 digits prime numbers. Then greedy | ||
all of them and use permutation to form new numbers. Use binary search | ||
to check if the permutated numbers is in our prime list and include | ||
them in a candidate list. | ||
|
||
After that, bruteforce all passed candidates sequences using | ||
3 nested loops since we know the answer will be 12 digits. | ||
The bruteforce of this solution will be about 1 sec. | ||
""" | ||
|
||
from itertools import permutations | ||
from math import floor, sqrt | ||
|
||
|
||
def is_prime(number: int) -> bool: | ||
""" | ||
function to check whether the number is prime or not. | ||
>>> is_prime(2) | ||
True | ||
>>> is_prime(6) | ||
False | ||
>>> is_prime(1) | ||
False | ||
Iqrar99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> is_prime(-800) | ||
False | ||
>>> is_prime(104729) | ||
True | ||
""" | ||
|
||
if number < 2: | ||
return False | ||
|
||
for i in range(2, floor(sqrt(number)) + 1): | ||
if number % i == 0: | ||
return False | ||
|
||
return True | ||
|
||
|
||
def search(target: int, prime_list: list) -> bool: | ||
""" | ||
function to search a number in a list using Binary Search. | ||
>>> search(3, [1, 2, 3]) | ||
True | ||
>>> search(4, [1, 2, 3]) | ||
False | ||
Iqrar99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> search(101, list(range(-100, 100))) | ||
False | ||
""" | ||
|
||
left, right = 0, len(prime_list) - 1 | ||
while left <= right: | ||
middle = (left + right) // 2 | ||
if prime_list[middle] == target: | ||
return True | ||
elif prime_list[middle] < target: | ||
left = middle + 1 | ||
else: | ||
right = middle - 1 | ||
|
||
return False | ||
|
||
|
||
def solution(): | ||
""" | ||
Return the solution of the problem. | ||
>>> solution() | ||
296962999629 | ||
""" | ||
prime_list = [n for n in range(1001, 10000, 2) if is_prime(n)] | ||
candidates = [] | ||
|
||
for number in prime_list: | ||
tmp_numbers = [] | ||
|
||
for prime_member in permutations(list(str(number))): | ||
prime = int("".join(prime_member)) | ||
|
||
if prime % 2 == 0: | ||
continue | ||
|
||
if search(prime, prime_list): | ||
tmp_numbers.append(prime) | ||
|
||
tmp_numbers.sort() | ||
if len(tmp_numbers) >= 3: | ||
candidates.append(tmp_numbers) | ||
|
||
passed = [] | ||
for candidate in candidates: | ||
length = len(candidate) | ||
found = False | ||
|
||
for i in range(length): | ||
for j in range(i + 1, length): | ||
for k in range(j + 1, length): | ||
if ( | ||
abs(candidate[i] - candidate[j]) | ||
== abs(candidate[j] - candidate[k]) | ||
and len(set([candidate[i], candidate[j], candidate[k]])) == 3 | ||
): | ||
passed.append( | ||
sorted([candidate[i], candidate[j], candidate[k]]) | ||
) | ||
found = True | ||
|
||
if found: | ||
break | ||
if found: | ||
break | ||
if found: | ||
break | ||
|
||
answer = set() | ||
for seq in passed: | ||
answer.add("".join([str(i) for i in seq])) | ||
|
||
return max([int(x) for x in answer]) | ||
|
||
|
||
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.