Skip to content

Commit

Permalink
prime constant pool works now
Browse files Browse the repository at this point in the history
  • Loading branch information
kspalaiologos committed Jan 14, 2024
1 parent 7e4c5f5 commit 38c7b99
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/main/java/rocks/palaiologos/maja/Maja.java
Original file line number Diff line number Diff line change
Expand Up @@ -4891,4 +4891,20 @@ public static double integrateGaussLegendreReal(BiFunction<Double, Double, Doubl
public static Complex integrateGaussLegendreComplex(BiFunction<Complex, Complex, Complex> f, Complex a, Complex b, Complex c, Complex d, int N) {
return integrateGaussLegendreComplex(x -> integrateGaussLegendreComplex(y -> f.apply(x, y), c, d, N), a, b, N);
}

/**
* Primality test for an integer in range [0, 2^31-1].
*/
public static boolean isPrime(int n) {
return Prime.is_prime_1b(n);
}

/**
* Primality test for an unsigned integer in range [0, 2^64-1].
* Notice that some values of n that will be interpreted as negative when signed
* may yield a truthy value, as the number is reinterpreted as unsigned.
*/
public static boolean isPrime(long n) {
return Prime.is_prime_2_64(n);
}
}
13 changes: 8 additions & 5 deletions src/main/java/rocks/palaiologos/maja/PrimeConstantPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -15,21 +16,23 @@ class PrimeConstantPool {
// and parse them into the arrays bases_2 and bases.
try {
InputStream in = PrimeConstantPool.class.getClassLoader().getResourceAsStream("maja_bases.txt");
String[] lines = new String(in.readNBytes(Integer.MAX_VALUE)).split(System.lineSeparator());
String[] bases_2_str = lines.get(0).split(",");
byte[] data = new byte[1024 * 1024];
int n = in.read(data);
String[] lines = new String(data, 0, n, StandardCharsets.US_ASCII).split(System.lineSeparator());
String[] bases_2_str = lines[0].split(",");
short[] bases_2 = new short[bases_2_str.length];
for (int i = 0; i < bases_2_str.length; ++i) {
bases_2[i] = Short.parseShort(bases_2_str[i]);
}
PrimeConstantPool.bases_2 = bases_2;
String[] bases_str = lines.get(1).split(",");
String[] bases_str = lines[1].split(",");
long[] bases = new long[bases_str.length];
for (int i = 0; i < bases_str.length; ++i) {
bases[i] = Long.parseLong(bases_str[i]);
bases[i] = Long.parseUnsignedLong(bases_str[i]);
}
PrimeConstantPool.bases = bases;
in.close();
} catch (IOException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/TestArithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,10 @@ public void testNChooseK() {
}
}
}

@Test
public void testPrime() {
for (int i = 0; i < 1000000; i++)
assertEquals(Maja.isPrime(i), Maja.isPrime((long) i));
}
}

0 comments on commit 38c7b99

Please sign in to comment.