-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoPrimes.java
More file actions
30 lines (27 loc) · 774 Bytes
/
CoPrimes.java
File metadata and controls
30 lines (27 loc) · 774 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
import java.util.BitSet;
public class CoPrimes {
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public int[] findCoPrimes(int num) {
BitSet candidates = new BitSet(num + 1);
candidates.set(1);
int coprime = 0;
for (int i = 2; i < num; i++) {
if (gcd(num, i) == 1) {
for (int j = candidates.nextSetBit(0); (coprime = i*j) < num; j = candidates.nextSetBit(j + 1)) {
candidates.set(coprime);
}
}
}
System.out.println(candidates);
return null;
}
public static void main(String[] args) {
CoPrimes cp = new CoPrimes();
int num = 36;
int[] coPrimes = cp.findCoPrimes(num);
}
}