mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2025-12-06 18:33:59 -05:00
Fixed a bug in getDivisors where a number would be
added twice
This commit is contained in:
@@ -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,8 +163,13 @@ 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);
|
||||
--Return the list
|
||||
|
||||
Reference in New Issue
Block a user