Module:Factorization

This template displays the factorization of a given number. Numbers smaller than 2 or greater than 1,000,000,000 return "number out of range". Fractional number…

This template displays the factorization of a given number. Numbers smaller than 2 or greater than 1,000,000,000 return "number out of range". Fractional numbers are rounded down.

Parameters
  • The first unnamed parameter is the number
  • Product - the symbol to be used to indicate times. Defaults to ·
  • Bold - set to any value to make it bold
  • Serif - set to any value to make it serif
  • Big - set to any value to make it big
  • Prime - set to any value to have prime numbers return an unformatted link to prime instead of the number

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

		-- Concat result and increment divisor
		-- when divisor is 2, go to 3. All other times, add 2
		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 number|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(numString)
    if bold then
    	numString = '<b>'..numString..'</b>'
    end

	ret = (serif or big) and '<span ' or ''
	if serif then ret = ret .. 'class="texhtml" ' end
	if big   then ret = ret .. 'style="font-size:165%" ' end
	ret = ret .. ((serif or big) and '>' or '') .. numString ..((serif or big) and '</span>' or '')

    return ret
end

return p


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.