Created
November 23, 2020 13:53
-
-
Save theothermattm/b9007a3465612ab7ba8cd2212dcd481a to your computer and use it in GitHub Desktop.
Java isPrime coding question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IsPrime { | |
public static void main(String args[]) { | |
System.out.println("6 (should be false):" + isPrime(6)); | |
System.out.println("11 (should be true):" + isPrime(11)); | |
// given a collection of numbers 11, 15, 2, 5, 6, 11 | |
// output the prime numbers, in sorted order, with no duplicates | |
} | |
public static boolean isPrime(Integer number) { | |
// Corner case | |
if (number <= 1) | |
return false; | |
// Check from 2 to n-1 | |
for (int i = 2; i < number; i++) | |
if (number % i == 0) | |
return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment