This is the current revision of this page, as edited by imported>WOSlinker at 14:38, 12 January 2020(fix closing span). The present address (URL) is a permanent link to this version.Revision as of 14:38, 12 January 2020 by imported>WOSlinker(fix closing span)
This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected.
This module displays the factorization of a given number. Numbers smaller than 2 or greater than 1,000,000,000 return "Error: <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
localp={}functionp.factor(frame)-- Consider calling the parser function #expr-- to simplify a potential mathematical expression?number=tonumber(frame.args[1])ifnumber==nilthenreturn'<strong class="error">Error: input not recognized as a number</strong>'endproductSymbol=frame.args['product']or'·'bold=frame.args['bold']andtruebig=frame.args['big']andtrueserif=frame.args['serif']andtrueprimeLink=frame.args['prime']andtruenumber=math.floor(number)ifnumber<2ornumber>1000000000ornumber==math.hugethenreturn'<strong class="error">Error: '..number..' out of range</strong>'endresult=""currentNumber=numberpower=0divisor=2-- Attempt factoring by the value of the divisor-- divisor increments by 2, except first iteration (2 to 3)whiledivisor<=math.sqrt(currentNumber)dopower=0whilecurrentNumber%divisor==0docurrentNumber=currentNumber/divisorpower=power+1end-- Concat result and increment divisor-- when divisor is 2, go to 3. All other times, add 2result=result..powerformat(divisor,power,productSymbol)divisor=divisor+(divisor==2and1or2)endifcurrentNumber~=1thenresult=result..currentNumber..' '..productSymbol..' 'endifcurrentNumber==numberandprimeLinkthenreturn'[[prime number|prime]]'endresult=string.sub(result,1,-4)returnformat(result)endfunctionpowerformat(divisor,power,productSymbol)ifpower<1thenreturn''elseifpower==1thenreturndivisor..' '..productSymbol..' 'elsereturndivisor..'<sup>'..power..'</sup> '..productSymbol..' 'endendfunctionformat(numString)ifboldthennumString='<b>'..numString..'</b>'endret=(seriforbig)and'<span 'or''ifserifthenret=ret..'class="texhtml" 'endifbigthenret=ret..'style="font-size:165%" 'endret=ret..((seriforbig)and'>'or'')..numString..((seriforbig)and'</span>'or'')returnretendreturnp