-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSundaram.java
More file actions
70 lines (59 loc) · 1.92 KB
/
Sundaram.java
File metadata and controls
70 lines (59 loc) · 1.92 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
import java.util.concurrent.Semaphore;
import java.util.ArrayList;
import java.lang.Math;
class MainSundaram{
public static void main(String[] args) {
long maxPrime = (long)Math.pow(2,37)-1;
Sundaram s = new Sundaram(maxPrime);
double time = System.nanoTime();
s.findPrimes();
time = (System.nanoTime() - time) / 1000000; //milliseconds
System.out.println("Time used to find primes: " + time + " ms.");
s.print();
}
}
public class Sundaram{
BitArr bitArr; // normal bit table for eratosthenes
byte[] bitMask = {(byte) 128, (byte) 64, (byte) 32, (byte) 16, (byte) 8, (byte) 4, (byte) 2, (byte) 1};
long maxPrime; //Storste verdi for primtall, n
final static int STARTPRIME = 3;
Sundaram(long maxPrime){
this.maxPrime = maxPrime;
bitArr = new BitArr((long)(maxPrime/2/8)+1);
}
public void findPrimes(){
long n = (long) (maxPrime/2)+1;
long q = 4;
long i, j;
for (i = 1; q < n; i++){
for (j = i; q < n; j++){
crossOut(q);
q = i + j + (2*i*j);
}
j = i;
q = i + j + (2*i*j);
}
}
// prints some of the biggest primes
public void print(){
long n = (long) (maxPrime/2)+1;
System.out.println("Primes:");
for (long i = n-100; i < n; i++){
if (!crossedOut(i)){
long p = (2*i)+1;
System.out.print(p + ", ");
}
}
System.out.println();
}
protected void crossOut(long n){
long index = n / 8;
int position = (int)(n - (index*8));
bitArr.set(index, (byte)(bitArr.get(index) | bitMask[position]));
}
protected boolean crossedOut(long n){
long index = n / 8;
int position = (int)(n - (index*8));
return !((bitArr.get(index) & bitMask[position]) != bitMask[position]);
}
}