Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate 100 factorial with all the digits

I came across a problem of calculating 100 factorial.

Here is what I tried first in Perl to calculate 100! :

#!/usr/bin/perl

use strict;
use warnings;
use Math::BigInt;

my $n=<>;
chomp($n);
print fac($n);

sub fac
{
    my ($m) = @_;

    return 1 if($m <=1 );
    return $m*fac($m-1);
}

But this is giving me 9.33262154439441e+157.

I need the answer with all of the digits.

What do I do?

like image 378
Krishnachandra Sharma Avatar asked Mar 21 '13 18:03

Krishnachandra Sharma


People also ask

How many digits do 100 factorial have?

It can be calculated easily using any programming Language. But Factorial of 100 has 158 digits. It is not possible to store these many digits even if we use "long long int".

How do you calculate 100 factorial?

The answer of what is the factorial of 100 The approximate value of 100! is 9.3326215443944E+157. The number of trailing zeros in 100! is 24. The number of digits in 100 factorial is 158.

How do you find the number of 100 digits?

If we write down the numbers from 1 to 100, we need to use 192 digits: one each for the number from 1 to 9, two each for each of the ninety numbers from 10 to 99, and three for the number 100.

How do you estimate the number of digits in a factorial?

As a rough approximation, multiplying an n-digit number by an m-digit number yields a result with about n+m digits. So the numbers from 2 to 9 are all 1-digit numbers. From 10 to 20 are all 2-digit numbers. That suggests we should have about 18 digits or so.


2 Answers

Doubles (which most Perls use) only have ~16 digits of precision. You need to use another system to get the 158 digits of precision you need.

use bigint;

This will cause Perl to automatically treat all numbers in your script as Math::BigInt objects.

If you need finer control (to treat some numbers as BigInt and some numbers as floating point) then see Krishnachandra Sharma's solution and explicitly use the Math::BigInt constructor.

Math::BigInt has a builtin factorial function, by the way:

$ perl -MMath::BigInt -e 'print Math::BigInt->bfac(100)'
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
like image 120
mob Avatar answered Sep 20 '22 08:09

mob


Doubles (which most Perls use) only have ~16 digits of precision. You need to another system to get the 158 digits of precision you need. Try using Math::BigInt.

Here is the code.

#!/usr/bin/perl

use strict;
use warnings;
use Math::BigInt;


my $n=100;
Math::BigInt->new($n);
print fac($n);

sub fac
{
    my ($m) = @_;

    return 1 if($m <=1 );
    return Math::BigInt->new($m*fac($m-1));
}

Produces 9332621544394415268169923e266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

like image 23
Krishnachandra Sharma Avatar answered Sep 22 '22 08:09

Krishnachandra Sharma