大数阶乘问题

日后整理

 1 import java.math.BigInteger;
 2 
 3 public class Factorial {
 4 
 5     public static void main(String[] args) {
 6         // System.out.println(recursion(0));
 7         // System.out.println(circulation(1));
 8         System.out.println(bigInteger(100));
 9     }
10 
11     /**
12      * 方法一 迭代实现阶乘
13      */
14     public static int recursion(int n) {
15         int sum = 1;
16         if (n < 0)
17             throw new IllegalArgumentException("必须为正整数");
18         if (n == 1 || n == 0) {
19             return sum;
20         } else {
21             sum = n * recursion(n - 1);
22             return sum;
23         }
24         // return sum;
25 
26     }
27 
28     /**
29      * 方法二 循环实现阶乘
30      */
31     public static int circulation(int n) {
32         int sum = 1;
33         if (n >= 0) {
34             if (n == 0 || n == 1) {
35                 return 1;
36             } else {
37                 int i = 1;
38                 while (i <= n) {
39                     sum = sum * n;
40                     n--;
41                 }
42                 return sum;
43             }
44         } else
45             throw new IllegalArgumentException("传入参数非法!");
46         // return sum;
47     }
48 
49     /**
50      * 方法三 利用BigInteger类
51      */
52 
53     public static BigInteger bigInteger(int n) {
54         BigInteger sum = new BigInteger("1");
55         if (n >= 0) {
56             if (n == 0 || n == 1) {
57                 return BigInteger.valueOf(1);
58             } else {
59                 int i = 1;
60                 while (i <= n) {
61                     sum = sum.multiply(BigInteger.valueOf(n));
62                     n--;
63                 }
64                 return sum;
65             }
66         } else
67             throw new IllegalArgumentException("传入参数非法!");
68     }
69 
70 }

 http://ly5633.iteye.com/blog/1219408

http://www.it165.net/pro/html/201409/21349.html

http://blog.csdn.net/liyong199012/article/details/40341779

原文地址:https://www.cnblogs.com/crazybuddy/p/5339360.html