forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBig.java
More file actions
44 lines (36 loc) · 1.32 KB
/
Copy pathBig.java
File metadata and controls
44 lines (36 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.math.BigInteger;
public class Big {
/**
* Computes the factorial function
* of a given BigInteger n.
* @param n the argument of the factorial function
* @return the value of the factorial function
*/
public static BigInteger factorial(BigInteger n){
BigInteger result = BigInteger.valueOf(1);
for(BigInteger i = n; i.compareTo(BigInteger.ONE) > 0; i = i.subtract(BigInteger.ONE)){
result = result.multiply(i);
}
return result;
}
public static void main(String[] args){
int x = 17;
BigInteger big = BigInteger.valueOf(x);
BigInteger small = BigInteger.valueOf(17);
BigInteger bigger = BigInteger.valueOf(1700000000);
BigInteger total = small.add(bigger);
BigInteger power = small.pow(x);
BigInteger product = small.multiply(big);
System.out.println(big);
System.out.println(small);
System.out.println(bigger);
System.out.println(total);
System.out.println(power);
System.out.println(product);
for(BigInteger i = BigInteger.ONE; i.compareTo(BigInteger.valueOf(31)) < 0; i = i.add(BigInteger.ONE)){
System.out.print(i + "\t");
System.out.print(factorial(i));
System.out.println();
}
}
}