For the ProjectEuler task, I wrote code that uses brute force to find the longest chain of primes below 100, which add up to prime, and the code really gives the correct results. Therefore, for numbers below 100, the answer is 2 + 3 + 5 + 7 + 11 + 13 = 41
import math
def prime(n):
for x in xrange(2,int(math.sqrt(n)+1)):
if n%x == 0:
return False
return True
primes = []
for x in xrange(2,100):
if prime(x):
primes += [x]
record = 0
i = 0
for num in primes:
i += 1
chain = [num]
for secnum in xrange(i,len(primes)-1):
chain += [primes[secnum]]
if len(chain) > record and sum(chain) in primes:
record = len(chain)
seq = chain
print seq
print seq
When I run this code, I get
[2, 3]
[2, 3, 5, 7]
[2, 3, 5, 7, 11, 13]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89]
This last line is very confusing for me. In my opinion, two print reports should give the same result. How was my seq variable assigned to this long list? The last list does not even meet the requirements of the if statement, in which seq is assigned. I'm sure this is a really stupid brain, but I just can't figure out what I messed up.