Module:Rational: Difference between revisions

Plumtree (talk | contribs)
mNo edit summary
Plumtree (talk | contribs)
m factorisation() implemented
Line 753: Line 753:
end
end
return c * 1200 / math.log(2)
return c * 1200 / math.log(2)
end
-- return prime factorisation of a rational number
function p.factorisation(a)
if type(a) == 'number' then
a = p.new(a)
end
if a.nan then return 'NaN' end
if a.inf then
local sign = '+'
if a.sign < 0 then
sign = '-'
end
return sign .. '∞'
end
if a.zero then return '0' end
local s = ''
if a.sign < 0 then
s = s .. '-'
end
local factors = {}
for factor, power in pairs(a) do
if type(factor) == 'number' then
table.insert(factors, factor)
end
end
table.sort(factors)
for i, factor in ipairs(factors) do
if i > 1 then s = s .. ' × ' end
s = s .. factor
if a[factor] ~= 1 then
s = s .. '<sup>' .. a[factor] .. '</sup>'
end
end
return s
end
end