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);