Module:Rational: Difference between revisions

Plumtree (talk | contribs)
mNo edit summary
Plumtree (talk | contribs)
m is_power() implemented
Line 441: Line 441:
end
end
return true
return true
end
-- determine whether a rational number is an integer power of another rational number
function p.is_power(a)
if type(a) == 'number' then
a = p.new(a)
end
if a.nan or a.inf or a.zero then
return false
end
if p.eq(a, 1) or p.eq(a, -1) then
return false
end
local function gcd(x, y)
if x < y then
x, y = y, x
end
while y > 0 do
x, y = y, x % y
end
return x
end
local total_power = nil
for factor, power in pairs(a) do
if type(factor) == 'number' then
if total_power then
total_power = gcd(total_power, math.abs(power))
else
total_power = math.abs(power)
end
end
end
return total_power > 1
end
end