Skip to content

Commit

Permalink
Merge pull request #1260 from akshat-jjain/patch-1
Browse files Browse the repository at this point in the history
Create Prime Checker.java
  • Loading branch information
fineanmol authored Nov 2, 2021
2 parents 21d060f + c03a01a commit abf4721
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 abf4721

Please sign in to comment.