-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_numbers.java
More file actions
52 lines (50 loc) · 1005 Bytes
/
p_numbers.java
File metadata and controls
52 lines (50 loc) · 1005 Bytes
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
class p_NumberDemo {
public static void main(String [] a) {
{
System.out.println("10 choose 4 is ");
System.out.println(new Numbers().choose(10, 4));
System.out.println("\nGCD of 84, 132 is ");
System.out.println(new Numbers().gcd(2035616, 64));
}
}
}
// The following code contains these legitimage syntax usage:
// - Variable declarations
// - Unary and complicated binary arithmetic operations
// - While loop
class Numbers {
public int choose(int n, int k) {
int res;
int i;
if (!(n<k)) {
// n! / (k! (n-k)!)
i = 1;
res = +1; // just to test unary +
while (i <= k) {
res = res * (n-k+i) / i;
i = i + 1;
}
} else {
res = 0;
}
return res;
}
public int gcd(int a, int b) {
int tmp;
int res;
if (a < b) {
tmp = a;
a = b;
b = tmp;
} else {
}
if (b == 0)
res = a;
else if ((a / b) * b != a) {
res = this.gcd(b, a-b*(a/b));
} else {
res = b;
}
return res;
}
}