RSS
 

ProjectEuler Problem 10

07 Dec

Find the sum of all the primes below two million.

my code:

#!/usr/bin/env python
firstPrime = 5
topCandidate = 2000000

primeList = []
candidate = 5
inc = 2
while(candidate <= topCandidate):
    position = 1
    thisPrime = firstPrime
    prime = 1
    while(thisPrime * thisPrime <= candidate):
	if(candidate % thisPrime == 0):
	    prime = 0
	    break
	thisPrime = primeList[position]
	position += 1
    if(prime):
	primeList.append(candidate)
    candidate = candidate + inc
    if(inc == 2):
	inc = 4
    else:
	inc = 2
print sum(primeList) + 5

素数是真锻炼人阿..

 
No Comments

Posted in scripts

 

Tags: , ,

Leave a Reply

 
Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).