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
|
--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));
|
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 you find one add it and the number it creates to the list
|
||||||
if((goalNumber % possibleDivisor) == 0) then
|
if((goalNumber % possibleDivisor) == 0) then
|
||||||
divisors[#divisors+1] = possibleDivisor;
|
divisors[#divisors+1] = possibleDivisor;
|
||||||
@@ -162,7 +163,12 @@ function getDivisors(goalNumber)
|
|||||||
if(possibleDivisor ~= topPossibleDivisor) then
|
if(possibleDivisor ~= topPossibleDivisor) then
|
||||||
divisors[#divisors + 1] =(goalNumber / possibleDivisor);
|
divisors[#divisors + 1] =(goalNumber / possibleDivisor);
|
||||||
end
|
end
|
||||||
|
--Take care of a few occations where a number was added twice
|
||||||
|
if(divisors(#divisors) == (possibleDivisor + 1)) then
|
||||||
|
possibleDivisor = possibleDivisor + 1;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
possibleDivisor = possibleDivisor + 1;
|
||||||
end
|
end
|
||||||
--Sort the list before returning for neatness
|
--Sort the list before returning for neatness
|
||||||
table.sort(divisors);
|
table.sort(divisors);
|
||||||
|
|||||||
Reference in New Issue
Block a user