Friday, September 7, 2012

Project Euler Problem 234

Python  Solution for http://projecteuler.net/problem=234 :

primesbelow(n) = return list of all primes below n

import primefacmodule as pfm
import time

start=time.time()

primes=pfm.primesbelow(1000004)
summe=[]
limit=999966663333
for p in xrange(1,len(primes)):
    l=[]
    x=0
    while (primes[p-1]**2+x*primes[p-1])<primes[p]**2:
        x+=1
        if  (primes[p-1]**2+x*primes[p-1])<primes[p]**2:
            if (primes[p-1]**2+x*primes[p-1])<=limit:
                l.append(primes[p-1]**2+x*primes[p-1])                
    x=0
    while (primes[p]**2-x*primes[p])>primes[p-1]**2:
        x+=1
        if  (primes[p]**2-x*primes[p])>primes[p-1]**2:
            if (primes[p]**2-x*primes[p])<=limit:
                l.append(primes[p]**2-x*primes[p])

    if primes[p-1]*primes[p]>=limit:
        summe.append(sum(l))
    else:
        summe.append(sum(l)-2*primes[p-1]*primes[p])

print sum(summe)
print time.time()-start # print execution time

No comments:

Post a Comment