Модуль:Factorization

Модуль отражает факторизацию любого числа большего, чем 1, и меньшего, чем 1000000000000000. Нецелые числа округляются в меньшую сторону.

Параметры:

  • 1= Число, для которого нужно вывести факторизаицю.



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'] or false

    number = math.floor(number)
    if number < 2 or number > 1000000000000000 then
        return '<strong class="error">число не поддерживается модулем [[Модуль:Factorization|Factorization]]</strong>'
    end

    result = ""
    currentNumber = number
    power = 0

    if currentNumber == 2 and primeLink then
    	return primeLink
    end
    
    --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..'<sup>'..power..'</sup>'..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..'<sup>'..power..'</sup>'..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 primeLink
    end

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

    return format(result)
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