-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
project_euler/problem_10 #1089
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
project_euler/problem_10 #1089
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
https://projecteuler.net/problem=10 | ||
|
||
Problem Statement: | ||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. | ||
|
||
Find the sum of all the primes below two million. | ||
|
||
using Sieve_of_Eratosthenes : | ||
|
||
The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million | ||
""" | ||
|
||
try: | ||
raw_input # Python 2 | ||
except NameError: | ||
raw_input = input # Python 3 | ||
|
||
try: | ||
xrange # Python 2 | ||
except NameError: | ||
xrange = range # Python 3 | ||
|
||
|
||
def prime_sum(n): | ||
"""Returns the sum of all the primes below n. | ||
|
||
|
||
>>> prime_sum(2000000) | ||
142913828922 | ||
>>> prime_sum(1000) | ||
76127 | ||
>>> prime_sum(5000) | ||
1548136 | ||
>>> prime_sum(10000) | ||
5736396 | ||
>>> prime_sum(7) | ||
10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests for negative number, floating point number, string? This will ensure that your code is robust and predictable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not for a negative number, floating-point number, string. it only works for positive numbers, thank you, |
||
|
||
""" | ||
|
||
list = [0 for i in xrange(n+1)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. list should not be used as a variable name in Python because there is list is a builtin that we should avoid shadowing. https://docs.python.org/3/library/functions.html |
||
|
||
list[0]=1 | ||
list[1]=1 | ||
|
||
for i in xrange(2,int(n**0.5)+1): | ||
if list[i]==0: | ||
for j in xrange(i * i, n+1, i): | ||
list[j]=1 | ||
s=0 | ||
for i in xrange(n): | ||
if list[i] == 0: | ||
s+=i | ||
return(s) | ||
|
||
if __name__ == '__main__': | ||
|
||
#import doctest | ||
#doctest.testmod() | ||
|
||
print(prime_sum(int(raw_input()))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. print(prime_sum(int(input().strip()))) will ensure that your code will gracefully deal with leading and/or trailing whitespace in user input. |
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to be a pain but... Let's simplify and just use input() and range() because this repo no longer supports legacy Python.