2

What is the sum of all prime numbers between 1,000,000,000,000 and 1,000,000,100,000? this works out but is very slow.I need to optimize it.I am new to python. 3614000181007876 is the right answer

    A=10 ** 6
N=A+1
B=10 ** 5
prime=[]
sum=0
for i in range(0,N):
    prime.append(0)
for i in range(2,N):
    if(prime[i]==1):
        continue
    for j in range(i*i,N,i):
        prime[j]=1
for i in range((A ** 2)+1,(A ** 2)+B,2):
    for j in range(2,A):
        c=0
        if(prime[j]==1):
            continue
        if(i%j==0):
            c=c+1
            if(c>0):
                break
    if(c==0):
        #print(i)
        sum=sum+i


print(sum)
  • 3
    Possible duplicates... http://stackoverflow.com/questions/10703699/program-to-find-all-primes-in-a-very-large-given-range-of-integers – Buddhima Gamlath Dec 26 '13 at 08:37
  • 1
    google "sieve of eratosthenes" – Vorsprung Dec 26 '13 at 08:38
  • 2
    seems relevant: http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python – Paul Dec 26 '13 at 08:39
  • 1
    The sum of all primes between 1,000,000,000,000 and 1,000,000,100,000 is 5042000251194180; using `primes` from my `bsdgames` Debian package as `primes 1000000000000 1000000100000|awk '{s += $1}END{print s}'` -computed in less than a hundredth of second – Basile Starynkevitch Dec 26 '13 at 08:39
  • I get the same result. bsdgames is available in Ubuntu as well. – Paul Dec 26 '13 at 08:42
  • 2
    @BasileStarynkevitch : 3614000181007876 is the right answer – user3131880 Dec 26 '13 at 08:59
  • Could you explain in the question how you know the correct answer? – Paul Dec 26 '13 at 09:11
  • @Paul Strangly enough, I get the same result as OP: 3614000181007876 – Hyperboreus Dec 26 '13 at 09:12
  • I tried finding the list of primes with "primes" from bsdgames and doing the sum in python and obtained 5041000251194141. This is different from what awk found, so maybe there is an issue. I note that bsdgames "primes" finds 5042 such primes. The first one being 1000000000039 and the last one being 1000000099979. I haven't checked if these are actually primes, just noting the nature of the output from this method. – Paul Dec 26 '13 at 09:19
  • @Paul I fear that 1000000099979 = 877853 * 1139143 and hence composite. Looks like a shitty primes package to me. – Hyperboreus Dec 26 '13 at 09:22
  • Yup, I see that too. I suppose primes is in "bsdgames" for a reason... I was curious. – Paul Dec 26 '13 at 09:26
  • 1
    I can confirm there are 3614 such primes and their sum is 3614000181007876. Takes 0.1 sec to calculate on a normal today's box (mine is slower, but I know the coefficient:)). The code is at http://www.haskell.org/haskellwiki/Prime_numbers#Above_Limit_-_Offset_Sieve. It's the "offset" sieve of Eratosthenes. Imperative pseudocode is [*here*](http://stackoverflow.com/a/19641049/849891). C code is [*here*](http://stackoverflow.com/a/9557173/849891). – Will Ness Dec 26 '13 at 09:45
  • there are 78498 primes below 1000000.05. that's all that we need, to sieve the interval from 10^12 to 10^12+10^5. – Will Ness Dec 26 '13 at 09:56
  • primes is not valid above 2**32 https://bugs.launchpad.net/ubuntu/+source/bsdgames/+bug/725367 – Pete Kirkham Dec 26 '13 at 14:26

1 Answers1

5

Not the most efficient way, but gets the right result (hopefully 3614000181007876) in 2 seconds on my box:

def isPrime(n):
    d = n - 1
    s = 0
    while not d & 1:
        s += 1
        d >>= 1
    for a in (2, 13, 23, 1662803):
        if pow(a, d, n) != 1 and all(pow(a, (1 << r) * d, n) != n - 1 for r in range(0, s)):
            return False
    return True

print(sum(x for x in range(1000000000001, 1000000100000, 2) if isPrime(x)))
Hyperboreus
  • 29,875
  • 7
  • 42
  • 78