diff --git a/lua/src/lib/primes.lua b/lua/src/lib/primes.lua index 3a2188cf..5b479a3e 100644 --- a/lua/src/lib/primes.lua +++ b/lua/src/lib/primes.lua @@ -26,11 +26,17 @@ local function primes(stop) local function next() if stop and prime >= stop then sieve = {} - error("Tried to exceed given limit") + return nil, "Tried to exceed given limit" end prime = next_prime(candidate) candidate = prime + 1 + + if stop and prime >= stop then + sieve = {} + return nil, "Tried to exceed given limit" + end + return prime end diff --git a/lua/src/p0003.lua b/lua/src/p0003.lua index e7ea1c4d..3554a240 100644 --- a/lua/src/p0003.lua +++ b/lua/src/p0003.lua @@ -11,13 +11,13 @@ local prime_factors = loadlib("primes").prime_factors return { solution = function() local fgen = prime_factors(600851475143) - local f = fgen.next() + local f local answer = 0 - while f do + repeat answer = f f = fgen.next() - end + until not f return answer end diff --git a/lua/src/p0010.lua b/lua/src/p0010.lua index f1b5b29d..315efba0 100644 --- a/lua/src/p0010.lua +++ b/lua/src/p0010.lua @@ -17,7 +17,7 @@ return { repeat answer = answer + tmp tmp = pg.next() - until tmp == nil + until not tmp return answer; end