-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler15.java
43 lines (36 loc) · 931 Bytes
/
Euler15.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
package Euler;
import java.math.BigInteger;
public class Euler15 {
public static long bCo(int n, int k){
if(k >(n-k))
k = n - k;
long c = 1;
for(int i = 0; i<k; i++){
c = c*(n-i);
c = c/(i+1);
}
return c;
}
public static BigInteger factorial(int n1){
BigInteger n = BigInteger.ONE;
for(int i = 1; i <= n1; i++){
n = n.multiply(BigInteger.valueOf(i));
}
return n;
}
public static void main(String[] args) {
long s = System.currentTimeMillis();
System.out.println(bCo(40,20));
long end = System.currentTimeMillis();
long time = end - s;
System.out.println(time+" ms");
long b = System.currentTimeMillis();
int n = 40;
int r = 20;
BigInteger num = factorial(r).multiply(factorial(n-r)); //n!/r!*(n-r)! (n choose r)
System.out.println(factorial(n).divide(num));
long e = System.currentTimeMillis();
long t = e-b;
System.out.println("time taken is "+t+" ms");
}
}