-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeCross.java
More file actions
216 lines (190 loc) · 5.9 KB
/
PrimeCross.java
File metadata and controls
216 lines (190 loc) · 5.9 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import java.io.File;
import java.lang.Math;
public class PrimeCross{
BitArr bitArr; // 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 = 9;
int[] smallPrimes = null;
long lastSqrt = -1;
PrimeCross(long maxPrime){
this.maxPrime = maxPrime;
System.out.println("maxPrime" + maxPrime);
bitArr = new BitArr((maxPrime/16)+1);
}
public long getMaxPrime(){
return maxPrime;
}
// finds primes with eratosthenes
public void findPrimes(){
crossOut(1); //Vi vet at 1 ikke er primtall...
cross357();
long product = STARTPRIME*STARTPRIME;
for (long k = STARTPRIME; product <= maxPrime; k +=2, product = k*k){
if(isPrime(k)){
long crossOutNumber = product;
long k2 = k*2;
while (crossOutNumber <= maxPrime){
crossOut(crossOutNumber);
crossOutNumber += k2;
}
}
}
}
// Checks for a number has factors or not. If not so, it is a prime
public boolean hasFactors (Int3 number){
if (number.dividable(2)){
return true;
}
for (int i = 0; i < smallPrimes.length; i++){
if (number.dividable(smallPrimes[i])){
if (number.equal(new Int3(smallPrimes[i]))){
return false;
} else{
return true;
}
}
}
long n = getLastSmall();
if (lastSqrt == -1){
Int3 tempNumber = number.clone();
if (tempNumber.isZero()){
tempNumber.addSmall(1);
}
if (tempNumber.greater(Long.MAX_VALUE)){
lastSqrt = Long.MAX_VALUE;
} else{
lastSqrt = tempNumber.toLong();
}
}
long sqrtNumber = number.sqrt(lastSqrt);
lastSqrt = sqrtNumber;
while (n <= (sqrtNumber+1)){
if (isPrime(n) && (number.dividable(n))){
return true;
}
n +=2;
}
return false;
}
public boolean hasFactorsTestLong(long number){
if (number % 2 == 0){
return true;
}
for (int i = 0; i < smallPrimes.length; i++){
if (number % smallPrimes[i] == 0){
if (number == smallPrimes[i]){
return false;
} else{
return true;
}
}
}
long n = getLastSmall();
if (lastSqrt == -1){
long tempNumber = number;
if (tempNumber == 0){
tempNumber++;
}
if (tempNumber > Long.MAX_VALUE){
lastSqrt = Long.MAX_VALUE;
} else{
lastSqrt = tempNumber;
}
}
long sqrtNumber = (long)Math.sqrt(number);
lastSqrt = sqrtNumber;
while (n <= (sqrtNumber+1)){
if (isPrime(n) && (number % n == 0)){
return true;
}
n +=2;
}
return false;
}
public long getLastSmall(){
if (smallPrimes == null || smallPrimes.length <= 0){
return STARTPRIME;
}
return smallPrimes[smallPrimes.length-1];
}
// prints some of the biggest primes
public void print(){
System.out.println("Primes:");
long n = maxPrime;
int counter = 0;
while (n <= maxPrime && counter < 5){
if (isPrime(n)){
System.out.print(n + ", ");
counter++;
}
n -=2;
}
}
public void printAll(){
System.out.println("Primes:");
long n = STARTPRIME;
while (n < maxPrime){
if (isPrime(n)){
System.out.print(n + ", ");
}
n +=2;
}
System.out.println();
}
// Returns the biggest prime from bitArr
public long getBiggestPrime(){
long n = maxPrime;
for (long i = n; i > 0; i-=2){
if (isPrime(i)){
return i;
}
}
return -1;
}
public long getPrevPrime(long n){
if (n % 2 == 0){
n--;
}
for (long i = n-2; i > 0; i-=2){
if (isPrime(i)){
return i;
}
}
return -1;
}
public long getNextPrime(long n){
if (n % 2 == 0){
n++;
}
for (long i = n+2; i > maxPrime; i+=2){
if (isPrime(i)){
return i;
}
}
return -1;
}
public boolean isPrime(long n){
long index = n / 16;
int position = (int)((n/2) - (index*8));
return ((bitArr.get(index) & bitMask[position]) != bitMask[position]);
}
protected void crossOut(long n){
long index = n / 16;
int position = (int)((n/2) - (index*8));
bitArr.set(index, (byte)(bitArr.get(index) | bitMask[position]));
}
protected void cross357(){
//0x49 = 01001001, 0x24= 00100100, 0x92 = 10010010.
byte[] crossOut3 = new byte[] {(byte)0x49, (byte)0x24, (byte)0x92};
// 0x21 = 00100001, 0x8 = 00001000, 0x42 = 01000010, 0x10 = 00010000, 0x84 = 10000100
byte[] crossOut5 = new byte[] {(byte)0x21, (byte)0x8, (byte)0x42, (byte)0x10, (byte)0x84};
// 00010000 00100000 01000000 10000001 00000010 00000100 00001000
byte[] crossOut7 = new byte[] {(byte)0x10, (byte)0x20, (byte)0x40, (byte)0x81, (byte)0x2, (byte)0x4, (byte)0x8};
for(long i = 0; i < bitArr.getLength(); i++){
bitArr.set(i, (byte) (crossOut3[(int)(i%3)] | crossOut5[(int)(i%5)] | crossOut7[(int)(i%7)]));
}
// 10001010 = 0x8A
bitArr.set(0, (byte) 0x8A);
}
}