Anonymous

Module:Shared: Difference between revisions

From Melvor Idle
Replace GCD function with an implementation of the Euclidean algorithm, so that fewer iterations occur when numbers differ greatly in magnitude
(Fixed specific edge case of rounding sometimes leading to too few digits being shown.)
(Replace GCD function with an implementation of the Euclidean algorithm, so that fewer iterations occur when numbers differ greatly in magnitude)
(One intermediate revision by one other user not shown)
Line 270: Line 270:
end
end


-- Euclid's Greatest Common Divisor algorithm
-- Euclidean Greatest Common Divisor algorithm
function p.gcd(a, b)
function p.gcd(a, b)
   if(a == b) then
   if b ~= 0 then
     return a
     return p.gcd(b, a % b)
   else
   else
     if(a > b) then
     return math.abs(a)
      if b == 0 then
        return a
      else
        return p.gcd(a - b, b)
      end
    else
      if a == 0 then
        return b
      else
        return p.gcd(a, b - a)
      end
    end
   end
   end
end
end
Line 355: Line 343:
   return result
   return result
end
end
 
function p.tablesEqual(t1, t2)
  if p.tableCount(t1) ~= p.tableCount(t2) then return false end
  for i, val in p.skpairs(t1) do
    if type(val) ~= type(t2[i]) then
      return false
    elseif type(val) == 'table' then
      if not p.tablesEqual(val, t2[i]) then return false end
    elseif t2[i] ~= val then
      return false
    end
  end
  return true
end
 
return p
return p