Skip to content

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

Merged
merged 4 commits into from
Jul 31, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions project_euler/problem_10/sol3.py
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
Copy link
Member

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.



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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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,
it a question base problem if anyone uses it, should only enter positive numbers.

thank you,


"""

list = [0 for i in xrange(n+1)]
Copy link
Member

Choose a reason for hiding this comment

The 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())))
Copy link
Member

Choose a reason for hiding this comment

The 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.