集册 Java实例教程 大整数的递归阶乘方法。

大整数的递归阶乘方法。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

391
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
大整数的递归阶乘方法。

import java.math.BigInteger;


public class Main/**时 代 J a v a 公 众 号 - nowjava.com**/

{

   // recursive method factorial (assumes its parameter is >= 0)

   public static BigInteger factorial(BigInteger number)

   {

      if (number.compareTo(BigInteger.ONE) <= 0) // test base case

         return BigInteger.ONE; 

      else // recursion step

         return number.multiply(factorial(number.subtract(BigInteger.ONE)));

   } 


   // output factorials for values 0-50

   public static void main(String[] args)

   {// from 时 代 J     a    v  a - nowjava.com

      // calculate the factorials of 0 through 50

      for (
展开阅读全文