Jump to content

Module:Factorization/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
add'l case....
consolidating divisor with subroutine (cleaner)
Line 29: Line 29:
while divisor <= math.sqrt(currentNumber) do
while divisor <= math.sqrt(currentNumber) do
power = 0
power = 0

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

while currentNumber % divisor == 0 do
while currentNumber % divisor == 0 do
currentNumber = currentNumber / divisor
currentNumber = currentNumber / divisor
Line 41: Line 34:
end
end


result = result .. powerformat(power, productSymbol)
result = result .. powerformat(divisor, power, productSymbol)


divisor = divisor + (divisor == 2 and 1 or 2)
divisor = divisor + (divisor == 2 and 1 or 2)
Line 59: Line 52:
end
end


function powerformat(power, productSymbol)
function powerformat(divisor, power, productSymbol)
if power < 1 then return ''
if power < 1 then return ''
elseif power == 1 then return ' '..productSymbol..' '
elseif power == 1 then return divisor .. ' ' .. productSymbol .. ' '
else return '<sup>'..power..'</sup>'..productSymbol..' '
else return divisor .. '<sup>' .. power .. '</sup>' .. productSymbol .. ' '
end
end
end
end

Revision as of 06:30, 24 April 2016

local p = {}

function p.factor(frame)
	-- Consider calling the parser function #expr
	--   to simplify a potential mathematical expression?
    number = tonumber(frame.args[1])
    if number == nil then
    	return '<strong class="error">Error: input not recognized as a number</strong>'
    end

    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 < 2 or number > 1000000000 or number == math.huge then
        return '<strong class="error">Error: ' .. number .. ' out of range</strong>'
    end

    result = ""
    currentNumber = number
    power = 0
    divisor = 2

    -- Attempt factoring by the value of the divisor
    --   divisor increments by 2, except first iteration (2 to 3)
    while divisor <= math.sqrt(currentNumber) do
        power = 0
        while currentNumber % divisor == 0 do
            currentNumber = currentNumber / divisor
            power = power + 1
        end

		result = result .. powerformat(divisor, power, productSymbol)

        divisor = divisor + (divisor == 2 and 1 or 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 powerformat(divisor, power, productSymbol)
	if power < 1      then return ''
    elseif power == 1 then return divisor .. ' ' .. productSymbol .. ' '
    else return divisor .. '<sup>' .. power .. '</sup>' .. productSymbol .. ' '
    end
end

function format(num)
    if bold then
        num = '<b>'..num..'</b>'
    end

    if serif then
        if big then
            num = '<span class="texhtml" style="font-size:165%">'..num..'</span>'
        else
            num = '<span class="texhtml">'..num..'</span>'
        end
    elseif big then
        num = '<span style="font-size:165%">'..num..'</span>'
    end

    return num
end

return p