Skip to content

Instantly share code, notes, and snippets.

@theothermattm
Created November 23, 2020 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theothermattm/b9007a3465612ab7ba8cd2212dcd481a to your computer and use it in GitHub Desktop.
Save theothermattm/b9007a3465612ab7ba8cd2212dcd481a to your computer and use it in GitHub Desktop.
Java isPrime coding question
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