Perl | Math::BigInt->bfac() method
Math::BigInt
module in Perl provides objects that represent integers with arbitrary precision and overloaded arithmetical operators.
bfac()
method of Math::BigInt
module is used to calculate factorial of a number stored as BigInt
object.
Syntax: Math::BigInt->bfac()
Parameter: None
Returns: a normalised BigInt object whose value represents the factorial of the given number.
Example 1: Use of Math::BigInt->bfac()
method
use Math::BigInt;
$num = 5;
$x = Math::BigInt->new( $num );
$factorial = $x ->bfac();
print "Factorial of $num : $factorial\n" ;
$num = 10;
$x = Math::BigInt->new( $num );
$factorial = $x ->bfac();
print "Factorial of $num : $factorial\n" ;
|
Output:
Factorial of 5 : 120
Factorial of 10 : 3628800
Example 2: Use of Math::BigInt->digit()
method to count digits in factorial of big numbers
use Math::BigInt;
$num = 100;
$x = Math::BigInt->new( $num );
$factorial = $x ->bfac();
print "Factorial of $num : $factorial \n" ;
$count = $factorial -> length ();
print "Count of digits in factorial : $count" ;
|
Output:
factorial of 100 : 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Count of digits in factorial : 158
Example 3: Use of Math::BigInt->digit()
method to calculate value of nCr.
use Math::BigInt;
$n = 5;
$r = 3;
$x = Math::BigInt->new( $n );
$y = Math::BigInt->new( $r );
$z = Math::BigInt->new( $n - $r );
$nCr = $x ->bfac() / ( $y ->bfac() * $z ->bfac());
print "Value of ${n}C${r} : $nCr \n" ;
$n = 50;
$r = 15;
$x = Math::BigInt->new( $n );
$y = Math::BigInt->new( $r );
$z = Math::BigInt->new( $n - $r );
$nCr = $x ->bfac() / ( $y ->bfac() * $z ->bfac());
print "Value of ${n}C${r} : $nCr \n" ;
|
Output:
Value of 5C3 : 10
Value of 50C15 : 2250829575120
Last Updated :
03 Oct, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...