JavaScript factorial prevents infinity

I used this function to calculate factorials in JavaScript:

var f = [];
function factorial (n) {
  if (n == 0 || n == 1)
    return 1;
  if (f[n] > 0)
    return f[n];
  return f[n] = factorial(n-1) * n;
}

Everything seemed to be going well until I tried the number 500. He returned infinity.

Is there a way to prevent infinityas an answer?

Thanks.

+4
source share
5 answers

You really need to use chips. Using math.js you can:

// configure math.js to work with enough precision to do our calculation
math.config({precision: 2000});

// evaluate the factorial using a bignumber value
var value = math.bignumber(500);
var result = math.factorial(value);

// output the results
console.log(math.format(result, {notation: 'fixed'}));

This will output:

1220136825991110068701238785423046926253574342803192842192413588385845373153881997605496447502203281863013616477148203584163378722078177200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062802310452644218878789465754777149863494367781037644274033827365397471386477878495438489595537537990423241061271326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338 175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

+6

500! , - , "[] ".

, , float , JavaScript .

, : p

EDIT: , , :

500!= 12201368259911100687012387854230469262535743428031928421924135883858453731538819976054964475022032818630136164771482035841633787220781772004807852051593292854779075719393306037729608590862704291745478824249127263443056701732707694610628023104526442188787894657547771498634943677810376442740338273653974713864778784954384895955375379904232410612713269843277457155463099772027810145610811883737095310163563244329870295638966289116589747695720879269288712817800702651745077684107196243903943225364226052349458501299185715012487069615681416253590566934238130088562492468915641267756544818865065938479517753608940057452389403357984763639449053130623237490664450488246650759467358620746379251842004593696929810222639719525971909452178233317569345815085523328207628200234026269078983424517120062077146409794561161276291459512372299133401695523638509428855920187274337951730145863575708283557801587354327688886801203998823847021514676054454076635359841744304801289383138968816394874696588175045069263653381 75055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

1,135 . , float 15 .

+2

, - ... . , , , . "" -. , . .

.

<!DOCTYPE html>

<html>
    <head>
        <title>Factorial</title>
        <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
    </head>

    <body>
        <input type='text' id='number' />
        <input type='button' value='!Factorial!' id='btn' />

        <script>
        var reslt=1;
        var counter=0;  
        var mantissa=0; //stores the seperated matissa 
        var exponent=0; //stores the seperated exponent

            $(document).ready(function (){
                $('#btn').click(function (){
                    var num=parseFloat($('#number').val()); //number input by user

                    for(i=1;i<=num;i++){
                        reslt=reslt*i;
                        //when the result becomes so high that the exponent reaches 306, the number is divided by 1e300
                        if((parseFloat(reslt.toExponential().toString().split("e")[1]))>=300){
                            reslt=reslt/1e300; //the result becomes small again to be able to be iterated without becoming infinity
                            counter+=1; //the number of times that the number is divided in such manner is recorded by counter
                        }
                    }

                    //the mantissa of the final result is seperated first
                    mantissa=parseFloat(reslt.toExponential().toString().split("e")[0]);
                    //the exponent of the final result is obtained by adding the remaining exponent with the previously dropped exponents (1e300)
                    exponent=parseFloat(reslt.toExponential().toString().split("e")[1])+300*counter;

                    alert(mantissa+"e+"+exponent); //displays the result as a string by concatenating

                    //resets the variables and fields for the next input if any
                    $('#number').val('');
                    reslt=1;
                    mantissa=0;
                    exponent=0;
                    counter=0;
                });
            });
        </script>
    </body>
</html>
+1
source

Some improve your code.

function factorial (n) {
  if (n === 0 || n === 1)
    return 1;

  return factorial(n-1) * n;
}
-4
source

All Articles