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

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl 
  
# Import Math::BigInt module
use Math::BigInt;
  
$num = 5;
  
# Calculate the factorial of
# the above specified number
  
# Create a BigInt object
$x = Math::BigInt->new($num);
  
# Use Math::BigInt->bfac() method
# to calculate factorial
$factorial = $x->bfac();
  
print "Factorial of $num : $factorial\n";
  
  
$num = 10;
  
# Calculate the factorial of
# the above specified number
  
# Create a BigInt object
$x = Math::BigInt->new($num);
  
# Use Math::BigInt->bfac() method
# to calculate factorial
$factorial = $x->bfac();
  
print "Factorial of $num : $factorial\n";

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl 
  
# Import Math::BigInt module
use Math::BigInt;
  
$num = 100;
  
# Calculate the factorial of
# the above specified number
  
# Create a BigInt object
$x = Math::BigInt->new($num);
  
# Use Math::BigInt->bfac() method
# to calculate factorial
$factorial = $x->bfac();
  
# Print the factorial
print "Factorial of $num : $factorial \n";
  
# Print count of digits in factorial
$count = $factorial->length();
print "Count of digits in factorial : $count";

chevron_right


Output:

factorial of 100 : 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 
Count of digits in factorial : 158

Example 3: Use of Math::BigInt->digit() method to calculate value of nCr.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl 
  
# Import Math::BigInt module
use Math::BigInt;
  
# Value of n
$n = 5;
  
# Value of r
$r = 3;
  
  
# Create BigInt objects 
$x = Math::BigInt->new($n);
$y = Math::BigInt->new($r);
$z = Math::BigInt->new($n - $r);
  
  
# calculate nCr using 
# formula n! / (r! * (n-r)!)
$nCr = $x->bfac() / ($y->bfac() * $z->bfac());
  
  
# Print calculated value of nCr
print "Value of ${n}C${r} : $nCr \n";
  
  
# Value of n
$n = 50;
  
# Value of r
$r = 15;
  
  
# Create BigInt objects 
$x = Math::BigInt->new($n);
$y = Math::BigInt->new($r);
$z = Math::BigInt->new($n - $r);
  
  
# calculate nCr using 
# formula n! / (r! * (n-r)!)
$nCr = $x->bfac() / ($y->bfac() * $z->bfac());
  
  
# Print calculated value of nCr
print "Value of ${n}C${r} : $nCr \n";

chevron_right


Output:

Value of 5C3 : 10 
Value of 50C15 : 2250829575120 



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.