Skip to content

Commit

Permalink
Create Prime Checker.java
Browse files Browse the repository at this point in the history
  • Loading branch information
akshat-jjain authored Oct 29, 2021
1 parent 9735ab5 commit c03a01a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Prime Checker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import static java.lang.System.in;

class Prime {
void checkPrime(int... numbers) {
for (int num : numbers) {
if (isPrime(num)) {
System.out.print(num + " ");
}
}
System.out.println();
}

boolean isPrime(int n) {
if (n < 2) {
return false;
} else if (n == 2) { // account for even numbers now, so that we can do i+=2 in loop below
return true;
} else if (n % 2 == 0) { // account for even numbers now, so that we can do i+=2 in loop below
return false;
}
int sqrt = (int) Math.sqrt(n);
for (int i = 3; i <= sqrt; i += 2) { // skips even numbers for faster results
if (n % i == 0) {
return false;
}
}
return true;
}
}

0 comments on commit c03a01a

Please sign in to comment.