Skip to content

Instantly share code, notes, and snippets.

@orbyfied
Last active December 17, 2021 10:41
Show Gist options
  • Save orbyfied/88e667a1b029b4c7933c84c65bc93fc2 to your computer and use it in GitHub Desktop.
Save orbyfied/88e667a1b029b4c7933c84c65bc93fc2 to your computer and use it in GitHub Desktop.
Primes Benchmark Java (JDK 17 w/ Gradle)
public class Bench {
static boolean isPrime(int n) {
float f = (float)Math.sqrt(n) + 1;
int i = 2;
while (i < f) {
if ((n % i) == 0)
return false;
i++;
}
return true;
}
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
int limit = 250001;
int nprimes = 0;
for (int i = 2; i < limit; i++) {
if (isPrime(i)) nprimes++;
}
long t2 = System.currentTimeMillis();
long t = t2 - t1;
System.out.println("Found " + nprimes + " primes <" + limit + "in " + t + "ms (" + t / 1000 + "sec)");
// yielded: /* Found 22043 primes <250001in 15ms (0sec) */
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment