mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-24 21:29:00 +00:00

Look, this will make the git history look funny, but trimming unnecessary depth from the source tree is a very necessary sanity-preserving measure when dealing with a super-modularized codebase like this one. While it makes the project configuration a bit less conventional, it will save you several clicks every time you jump between modules. Which you'll do a lot, because it's *modul*ar. The src/main/java convention makes a lot of sense for a non-modular project though. This ain't that.
43 lines
958 B
Java
43 lines
958 B
Java
package nu.marginalia.util;
|
|
|
|
public class NextPrimeUtil {
|
|
|
|
/** Returns the next prime value starting at start. If start is prime, return start.
|
|
*/
|
|
public static long nextPrime(long start, long direction) {
|
|
if (isCoprime(start, 2)) {
|
|
start = start + direction;
|
|
}
|
|
|
|
long val;
|
|
for (val = start; !isPrime(val); val += 2*direction) {}
|
|
return val;
|
|
}
|
|
|
|
public static boolean isPrime(long v) {
|
|
if (v <= 2) {
|
|
return true;
|
|
}
|
|
if ((v & 1) == 0) {
|
|
return false;
|
|
}
|
|
for (long t = 3; t <= v/3; t++) {
|
|
if ((v % t) == 0) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static boolean isCoprime(long a, long b) {
|
|
if (a == 0 || b == 0) {
|
|
return false;
|
|
}
|
|
|
|
if (a > b) {
|
|
return (a % b) == 0;
|
|
}
|
|
return (b % a) == 0;
|
|
}
|
|
}
|