From b0ba590c53f3ddb99863f2b05d32b0caefa01abb Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 22 Mar 2019 19:06:09 -0400 Subject: [PATCH] Fixed a bug in getDivisors where a number would be added twice --- Algorithms.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Algorithms.lua b/Algorithms.lua index 6bb6e82..b219ba9 100644 --- a/Algorithms.lua +++ b/Algorithms.lua @@ -154,7 +154,8 @@ function getDivisors(goalNumber) --Start at 3 and loop through all numbers < (goalNumber / 2) looking for a number that divides it evenly local topPossibleDivisor = math.ceil(math.sqrt(goalNumber)); - for possibleDivisor=2,topPossibleDivisor do + local possibleDivisor = 2; + while(possibleDivisor <= topPossibleDivisor) do --If you find one add it and the number it creates to the list if((goalNumber % possibleDivisor) == 0) then divisors[#divisors+1] = possibleDivisor; @@ -162,7 +163,12 @@ function getDivisors(goalNumber) if(possibleDivisor ~= topPossibleDivisor) then divisors[#divisors + 1] =(goalNumber / possibleDivisor); end + --Take care of a few occations where a number was added twice + if(divisors(#divisors) == (possibleDivisor + 1)) then + possibleDivisor = possibleDivisor + 1; + end end + possibleDivisor = possibleDivisor + 1; end --Sort the list before returning for neatness table.sort(divisors);