Skip to content

Commit 263de28

Browse files
ch10p2
1 parent e3666d9 commit 263de28

5 files changed

Lines changed: 77 additions & 5 deletions

File tree

‎.idea/copilot.data.migration.agent.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/copilot.data.migration.ask.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/copilot.data.migration.edit.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ch10/Big.java‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
1+
import java.math.BigInteger;
2+
13
public class Big {
24

35
/**
46
* Computes the factorial function
5-
* of a given integer n.
7+
* of a given BigInteger n.
68
* @param n the argument of the factorial function
79
* @return the value of the factorial function
810
*/
9-
public static int factorial(int n){
10-
int result = 1;
11-
for(int i=n;i > 1; i--){
12-
result *= i;
11+
public static BigInteger factorial(BigInteger n){
12+
BigInteger result = BigInteger.valueOf(1);
13+
for(BigInteger i = n; i.compareTo(BigInteger.ONE) > 0; i = i.subtract(BigInteger.ONE)){
14+
result = result.multiply(i);
1315
}
1416
return result;
1517
}
18+
19+
public static void main(String[] args){
20+
int x = 17;
21+
BigInteger big = BigInteger.valueOf(x);
22+
BigInteger small = BigInteger.valueOf(17);
23+
BigInteger bigger = BigInteger.valueOf(1700000000);
24+
BigInteger total = small.add(bigger);
25+
BigInteger power = small.pow(x);
26+
BigInteger product = small.multiply(big);
27+
System.out.println(big);
28+
System.out.println(small);
29+
System.out.println(bigger);
30+
System.out.println(total);
31+
System.out.println(power);
32+
System.out.println(product);
33+
34+
for(BigInteger i = BigInteger.ONE; i.compareTo(BigInteger.valueOf(31)) < 0; i = i.add(BigInteger.ONE)){
35+
System.out.print(i + "\t");
36+
System.out.print(factorial(i));
37+
System.out.println();
38+
}
39+
}
1640
}
41+
42+
43+
44+

‎ch10/Pow.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.math.BigInteger;
2+
13
/**
24
* BigInteger exercise.
35
*/
@@ -21,4 +23,28 @@ public static int pow(int x, int n) {
2123
}
2224
}
2325

26+
/**
27+
* BigInteger exponentiation.
28+
*/
29+
public static BigInteger powBig(int x, int n) {
30+
if (n == 0) return BigInteger.ONE;
31+
32+
// find x to the n/2 recursively
33+
BigInteger t = powBig(x, n / 2);
34+
35+
// if n is even, the result is t squared
36+
// if n is odd, the result is t squared times x
37+
if (n % 2 == 0) {
38+
return t.multiply(t);
39+
} else {
40+
return t.multiply(t).multiply(BigInteger.valueOf(x));
41+
}
42+
}
43+
44+
public static void main(String[] args){
45+
System.out.println(pow(4,5));
46+
System.out.println(powBig(4,5));
47+
System.out.println(powBig(4,123));
48+
}
49+
2450
}

0 commit comments

Comments
 (0)