Module:Factorization

local p = {}

function p.factor(frame) number = tonumber(frame.args[1]) productSymbol = frame.args['product'] or '·' bold = frame.args['bold'] and true big = frame.args['big'] and true serif = frame.args['serif'] and true primeLink = frame.args['prime'] and true

number = math.floor(number) if number 1000000000 then return ' number out of range ' end

result = "" currentNumber = number power = 0 --First take care of 2, the only even prime number. if currentNumber % 2 == 0 then result = '2' currentNumber = currentNumber / 2 power = 1 end

while currentNumber % 2 == 0 do       currentNumber = currentNumber / 2 power = power + 1 end

if power > 1 then result = result....power....productSymbol..' ' elseif power == 1 then result = result..' '..productSymbol..' ' end

--Now do all odd numbers. prime = 3

while prime <= math.sqrt(currentNumber) do

power = 0

if currentNumber % prime == 0 then result = result..prime currentNumber = currentNumber / prime power = 1 end

while currentNumber % prime == 0 do           currentNumber = currentNumber / prime power = power + 1 end

if power > 1 then result = result....power....productSymbol..' ' elseif power == 1 then result = result..' '..productSymbol..' ' end

prime = prime + 2 end

if currentNumber ~= 1 then result = result..currentNumber..' '..productSymbol..' ' end

if currentNumber == number and primeLink then return ..'prime'.. end

result = string.sub(result,1,-4)

return format(result) end

function format(num) if bold then num = ..num.. end

if serif then if big then num = ' '..num..' ' else num = ' '..num..' ' end elseif big then num = ' '..num..' ' end

return num end

return p