Monday, 19 August 2013

Lua integer won't increment

Lua integer won't increment

I have this simple lua function designed to solve the problem of
consecutive prime sum. The prime 41, can be written as the sum of six
consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum
of consecutive primes that adds to a prime below one-hundred. this is my
function:
function numOfConsecPrimes(limit)
a = allPrimes(limit/2)
length = table.getn(a)
sumSoFar = 0 innerSum = 0 finalSum = 0
pos = 1
items = 0 innerItems = 0 finalItems = 0
resetpos = pos
while resetpos < length do
pos = resetpos
resetpos = resetpos + 1
items = 0
sumSoFar = 0
while sumSoFar < limit and pos < length do
if isPrime(sumSoFar) == true then innerSum = sumSoFar innerItems =
items end
print(sumSoFar)
sumSofar = sumSoFar + a[pos]
print(a[pos] .."->"..sumSoFar)
pos = pos + 1
items = items + 1
end
if innerItems > finalItems then finalItems = innerItems finalSum =
innerSum end
end
end
But for some reason, sumSoFar just won't change. I'm printing it before
and after the addition of a[pos] and it stays zero always. I'm printing
a[pos] as you see and the values are fine. So what's going on?

No comments:

Post a Comment