-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4129517
commit 77f924f
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,28 @@ | ||
#The sum of the primes below 10 is 2+3+5+7=17 | ||
#Find the sum of all the primes below two million | ||
|
||
pmax = 2000000 | ||
|
||
#checks if integer is prime | ||
def isprime(x): | ||
"Check if integer x is prime, return True or False" | ||
if x == 2: | ||
return True | ||
elif x < 2 or not x & 1: | ||
return False | ||
for i in range(3, int(x**0.5)+1, 2): | ||
if x%i == 0: | ||
return False | ||
return True | ||
|
||
#store all the primes below pmax as a list | ||
primelist = filter(isprime, range(1,pmax)) | ||
|
||
#sum all the primes in the list | ||
psum = 0 | ||
for i in primelist: | ||
psum += int(i) | ||
|
||
print('The sum of all the primes below two million is', psum) | ||
|
||
#Answer: 142913828922 |