-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Hacktoberfest: Add a solution for Project Euler 50 #2703
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
Closed
Closed
Changes from 16 commits
Commits
Show all changes
20 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 cb8ab38
updating DIRECTORY.md
7824282
Add problem 50 solution
Iqrar99 8dbef02
Merge branch 'pe-50' of https://github.com/Iqrar99/Python into pe-50
Iqrar99 c933fe8
updating DIRECTORY.md
a2acaf8
Fix some mistakes
Iqrar99 f4b32b2
Merge branch 'pe-50' of https://github.com/Iqrar99/Python into pe-50
Iqrar99 e75158b
Move the import statement lower
Iqrar99 39622e7
Merge branch 'master' into pe-50
Iqrar99 eebca1e
Add default argument into main function
Iqrar99 276feaa
Merge branch 'pe-50' of https://github.com/Iqrar99/Python into pe-50
Iqrar99 c45bfe3
Improve the code
Iqrar99 e51429f
Small fix
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,102 @@ | ||
""" | ||
Consecutive prime sum | ||
|
||
Problem 50 | ||
|
||
The prime 41, can be written as the sum of six consecutive primes: | ||
|
||
41 = 2 + 3 + 5 + 7 + 11 + 13 | ||
|
||
This is the longest sum of consecutive primes that adds to | ||
a prime below one-hundred. | ||
|
||
The longest sum of consecutive primes below one-thousand that adds to a prime, | ||
contains 21 terms, and is equal to 953. | ||
|
||
Which prime, below one-million, can be written as the sum of | ||
the most consecutive primes? | ||
|
||
Solution: | ||
|
||
First of all, we need to generate all prime numbers | ||
from 2 to the closest prime number with 1000000. | ||
Then, use sliding window to get the answer. | ||
""" | ||
|
||
from math import floor, sqrt | ||
|
||
|
||
def is_prime(number: int) -> bool: | ||
""" | ||
function to check whether a number is a prime or not. | ||
>>> is_prime(2) | ||
True | ||
>>> is_prime(6) | ||
False | ||
>>> is_prime(1) | ||
False | ||
>>> is_prime(-7000) | ||
False | ||
>>> is_prime(104729) | ||
True | ||
""" | ||
|
||
if number < 2: | ||
return False | ||
|
||
for n in range(2, floor(sqrt(number)) + 1): | ||
if number % n == 0: | ||
return False | ||
|
||
return True | ||
|
||
|
||
def solution(): | ||
""" | ||
Return the problem solution. | ||
>>> solution() | ||
997651 | ||
Iqrar99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
prime_list = [2] + [x for x in range(3, 10 ** 6, 2) if is_prime(x)] | ||
|
||
cumulative_sum = [] | ||
tmp = 0 | ||
for prime in prime_list: | ||
tmp += prime | ||
if tmp < 10 ** 6: | ||
cumulative_sum.append(tmp) | ||
else: | ||
break | ||
|
||
upper_limit_idx = 0 | ||
for i in range(len(prime_list)): | ||
if prime_list[i] < 10 ** 6: | ||
upper_limit_idx = i | ||
else: | ||
break | ||
|
||
max_count = -1 | ||
answer = 0 | ||
for number in reversed(cumulative_sum): | ||
count_prime = cumulative_sum.index(number) + 1 | ||
|
||
if not is_prime(number): | ||
tmp = number | ||
|
||
for i in range(upper_limit_idx): | ||
count_prime -= 1 | ||
tmp -= prime_list[i] | ||
|
||
if is_prime(tmp) or tmp < 0: | ||
break | ||
|
||
if max_count < count_prime: | ||
max_count = count_prime | ||
answer = tmp | ||
|
||
return 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.