当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


perl Math::BigInt->bfac()用法及代码示例


Math::BigIntPerl中的module提供了代表具有任意精度的整数和重载算术运算符的对象。

bfac()的方法Math::BigInt模块用于计算存储为BigInt Object 。

用法: Math::BigInt->bfac()

参数:没有

返回:一个标准化的BigInt对象,其值表示给定数字的阶乘。

范例1:用于Math::BigInt->bfac()方法

#!/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";
输出:
Factorial of 5:120
Factorial of 10:3628800

范例2:用于Math::BigInt->digit()大数阶乘中的数字计数方法

#!/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";
输出:
factorial of 100:93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 
Count of digits in factorial:158

范例3:用于Math::BigInt->digit()nCr值的计算方法。

#!/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";
输出:
Value of 5C3:10 
Value of 50C15:2250829575120 


相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Perl | Math::BigInt->bfac() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。