-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrimeList.java
60 lines (55 loc) · 1.56 KB
/
PrimeList.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
54
55
56
57
58
59
60
import java.util.ArrayList;
/**
* Created by glars on 6/16/2016.
*/
public class PrimeList {
ArrayList<Integer> primes;
int operations;
public PrimeList() {
primes = new ArrayList<>();
operations = 0;
}
/**
* Checks for primes and adds them to list
* @param numbers to be parsed
*/
public void add(int[] numbers) {
if (numbers == null) {
throw new java.lang.IllegalArgumentException("Data is null");
}
// for every number check if prime
for (int i: numbers) {
if (checkPrime(i)) {
// Uncommenting this prints out every prime number
// System.out.println(i);
primes.add(i);
}
}
}
/**
* Checks if number is prime
* @param number to be checked
* @return true if prime, false if composite
*/
public boolean checkPrime(int number) {
int maxPossibleRoot = (int) Math.sqrt(number);
for (int i = 1; i < maxPossibleRoot; i++) {
operations++;
if (number % primes.get(i - 1) == 0) {
return false;
}
}
return true;
}
/**
* Calculates time complexity of operations
* @return x, where O(n^x)
*/
public double timeComplexity() {
double n = operations / primes.size();
return Math.log(operations) / Math.log(n);
}
public String toString() {
return primes.get(primes.size() - 1).toString();
}
}