hytromo
hytromo

Reputation: 1531

Project Euler #10 - Python Wrong result

Project Euler #10

I am using the following code:

import math

def is_prime(num):
    num_sqrt=int(math.sqrt(num))+1
    for i in range(2, num_sqrt):
        if(num%i == 0):
            return False
    return True

prime_sum=3
counter=2

for a in range(5, 2000000, 2):
    if(is_prime(a)):
        counter = counter +1
        print a, counter
        prime_sum=prime_sum+a

print prime_sum

And I get as a result the 142913828920

The last 10 lines of the output of the above program are:

1999859 148925
1999867 148926
1999871 148927
1999889 148928
1999891 148929
1999957 148930
1999969 148931
1999979 148932
1999993 148933
142913828920

The website http://www.numberempire.com/primenumbers.php confirms that 1999993 is the 148933rd prime (-> http://www.numberempire.com/primenumbers.php?number=1999993&action=check).

What's wrong here?

Upvotes: 0

Views: 276

Answers (2)

Gerry
Gerry

Reputation: 1323

It violates the spirit of Project Euler to post code purporting to be a whole solution (or even an attempt at a whole solution). It's an even greater breach of spirit to post an answer.

imho.

Gerry

Upvotes: 1

Mike Zboray
Mike Zboray

Reputation: 40818

You are initializing your variable prime_sum incorrectly. The first two primes are 2 and 3 so it should be 5 (not 3). In modern mathematics, 1 is not usually considered a prime.

Upvotes: 3

Related Questions