-
Notifications
You must be signed in to change notification settings - Fork 0
/
PascalTriangle.java
55 lines (41 loc) · 1.34 KB
/
PascalTriangle.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class PascalTriangle {
static BigInteger factorial(int n) {
BigInteger res = new BigInteger("1");
int i;
for (i = 2; i <= n; i++)
res = res.multiply(BigInteger.valueOf(i));
return res;
}
static long ncr(int n, int r) {
BigInteger factorialN = factorial(n);
BigInteger factorialR = factorial(r);
BigInteger factorialNR = factorial(n - r);
return factorialN.divide(factorialR.multiply(factorialNR)).intValue();
}
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
if (numRows == 0) {
return res;
}
for (int i = 0; i < numRows; i++) {
List<Integer> list = new ArrayList<>();
for (int j = 0; j <= i; j++) {
list.add((int) ncr(i, j));
}
// System.out.println(list);
res.add(list);
list.clear();
}
return res;
}
public static void main(String[] args) {
PascalTriangle pt = new PascalTriangle();
System.out.println(pt.generate(22));
// Scanner sc = new Scanner(System.in);
// int input = sc.nextInt();
// System.out.println(ncr(13,1));
}
}