-
Notifications
You must be signed in to change notification settings - Fork 16
/
ThreeDivisors.java
49 lines (40 loc) · 1.21 KB
/
ThreeDivisors.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
// https://leetcode.com/problems/three-divisors
// T: O(1)
// S: O(1)
public class ThreeDivisors {
private static final PrimeSieve PRIME_SIEVE = PrimeSieve.ofSize(101);
public boolean isThree(int n) {
return isPerfectSquare(n);
}
private boolean isPerfectSquare(int x) {
int sqrt = (int) Math.sqrt(x);
return sqrt * sqrt == x && PRIME_SIEVE.isPrime(sqrt);
}
private static class PrimeSieve {
final byte[] sieve;
private static PrimeSieve ofSize(int size) {
return new PrimeSieve(size);
}
PrimeSieve(int size) {
this.sieve = new byte[size + 1];
initializeSieve();
}
public boolean isPrime(int number) {
return sieve[number] == 0;
}
private void initializeSieve() {
sieve[0] = 1;
sieve[1] = 1;
for (int i = 2 ; i < sieve.length ; i++) {
if (sieve[i] == 0) {
markFactors(i);
}
}
}
private void markFactors(int prime) {
for (int i = prime * prime ; i < sieve.length ; i += prime) {
sieve[i] = 1;
}
}
}
}