當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。