-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimes.java
More file actions
executable file
·89 lines (77 loc) · 2.15 KB
/
Copy pathPrimes.java
File metadata and controls
executable file
·89 lines (77 loc) · 2.15 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Primes{
public static boolean isPrime(int n){
if (n < 2) {
return false;
}
for (int i = 2;i <= Math.sqrt(n) ; i++){
if (n % i == 0)
return false;
}
return true;
}
static ArrayList<Integer> primes = new ArrayList<Integer>();
static int sieveLimit = 1;
static void SieveOfEratosthenes(int maxSize) {
boolean[] isPrime = new boolean[maxSize + 1];
for (int i = 2; i <= maxSize; i++) {
isPrime[i] = true;
}
for (int p = 2; p * p <= maxSize; p++) {
if (isPrime[p]) {
for (int i = p * p; i <= maxSize; i += p) {
isPrime[i] = false;
}
}
}
primes.clear();
for (int p = 2; p <= maxSize; p++) {
if (isPrime[p]) {
primes.add(p);
}
}
sieveLimit = maxSize;
}
private static int upperBoundForNthPrime(int n) {
if (n < 6) {
return 15;
}
double dn = n;
return (int)Math.ceil(dn * (Math.log(dn) + Math.log(Math.log(dn)))) + 10;
}
public static int kthPrime(int k){
if (k < 0) {
throw new IllegalArgumentException("k must be non-negative");
}
if (primes.size() <= k) {
int limit = Math.max(sieveLimit, upperBoundForNthPrime(k + 1));
while (true) {
SieveOfEratosthenes(limit);
if (primes.size() > k) {
break;
}
limit *= 2;
}
}
return primes.get(k);
}
public static List<Integer> factorize(int n) {
List<Integer> factorsList = new ArrayList<>();
while (n % 2 == 0){
factorsList.add(2);
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i+= 2) {
while (n % i == 0) {
factorsList.add(i);
n /= i;
}
}
if (n > 2) {
factorsList.add(n);
}
return factorsList;
}
}