Skip to content

Instantly share code, notes, and snippets.

@maksverver
Created August 19, 2016 20:33
Show Gist options
  • Save maksverver/fbb6c0417f8c8bfc112345ecf9f6448b to your computer and use it in GitHub Desktop.
Save maksverver/fbb6c0417f8c8bfc112345ecf9f6448b to your computer and use it in GitHub Desktop.
class A {
public static boolean isPrime(int i) {
if (i < 2) return false;
for (int j = 2; j <= (int) Math.sqrt(i); ++j) {
if (i % j == 0) return false;
}
return true;
}
public static void main(String... args) {
int count = 0;
for (int i = 0; i < 10_000_000; ++i) {
if (isPrime(i)) ++count;
}
System.out.println(count);
}
}
class B {
public static boolean isPrime(int i) {
if (i < 2) return false;
final int limit = (int)Math.sqrt(i);
for (int j = 2; j <= limit; ++j) {
if (i % j == 0) return false;
}
return true;
}
public static void main(String... args) {
int count = 0;
for (int i = 0; i < 10_000_000; ++i) {
if (isPrime(i)) ++count;
}
System.out.println(count);
}
}
class C {
public static boolean isPrime(int i) {
if (i < 2) return false;
for (int j = 2; j*j <= i; ++j) {
if (i % j == 0) return false;
}
return true;
}
public static void main(String... args) {
int count = 0;
for (int i = 0; i < 10_000_000; ++i) {
if (isPrime(i)) ++count;
}
System.out.println(count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment