-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeMap.java
53 lines (42 loc) · 1.22 KB
/
PrimeMap.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
import java.util.HashMap;
public class PrimeMap<AnyType> {
HashMap<AnyType, Integer> primeMap;
private int[] primes;
public PrimeMap(){
primeMap = null;
primes = null;
}
public PrimeMap(int size){
primeMap = new HashMap<AnyType, Integer>();
this.primes = new int[size];
fillPrimes(this.primes);
}
public PrimeMap(AnyType[] array){
primeMap = new HashMap<AnyType, Integer>();
this.primes = new int[array.length];
fillPrimes(this.primes);
int counter = 0;
for (AnyType item : array) {
primeMap.put(item, this.primes[counter++]);
}
}
public void fillPrimes(int[] primes){
int counter = 0;
for (int i = 2; counter < primes.length; i ++){
if (isPrime(i)){
primes[counter++] = i;
}
}
}
public boolean isPrime(int number){
if (number == 0 || number == 1) return false;
if (number == 2) return true;
for (int i = 2; i <= Math.sqrt(number); i ++) {
if (number % i == 0) return false;
}
return true;
}
public int getPrime(AnyType c){
return primeMap.get(c);
}
}