I’d like to work out a faster way to do this problem. As it is, this is spending more than half its time determining which numbers are prime. I am assuming that it is double checking some of them.
Therefore, as it is, this script takes way too long to run. But, it works at least.
def generating(n):
if (isprime(n + 1) == False) or (n ** 0.5 == 0):
return False
else:
for d in xrange(2, int(n ** 0.5) + 1):
if (n % d == 0) and (isprime(d + (n / d)) == False):
return False
else:
return True
def isprime(n):
if n == 2:
return True
elif n < 2 or n%2 == 0:
return False
else:
for i in xrange(3, int(n**0.5 + 1), 2):
if n%i == 0: return False
return True
def Test():
assert generating(30) == True
print 'tests pass'
def Main():
Test()
bound = 10**8
total = 0
for n in xrange(bound):
if n%10**4 == 0: print n
if generating(n) == True:
total += n
print total
Main()