Skip to content

Instantly share code, notes, and snippets.

@notionquest
Created May 30, 2017 21:00
Show Gist options
  • Save notionquest/b78e7df382fd823c9fb7742b478baf9d to your computer and use it in GitHub Desktop.
Save notionquest/b78e7df382fd823c9fb7742b478baf9d to your computer and use it in GitHub Desktop.
Prime Number using Java 8 Stream
public boolean isPrime(int inputValue) {
if (inputValue <= 1) {
return false;
}
if (inputValue == 2) {
return true;
}
return !(IntStream.rangeClosed(2, inputValue / 2).boxed().anyMatch(i -> (inputValue % i) == 0));
}
@notionquest
Copy link
Author

Test cases :-

{ 1, false }, { 2, true }, { 3, true }, { 4, false }, { 5, true },
{ 6, false }, { 7, true }, { 8, false }, { 9, false }, { 10, false }, { 11, true }, { 13, true },
{ 15, false }, { 17, true }, { 19, true }, { 21, false }, { 23, true }, { 24, false }, { 25, false },
{ 27, false }, { 29, true }, { 31, true }, { 33, false }, { 35, false }, { 37, true }, { 39, false },
{ 379009, true }, { 608999999, true }, { 60899999, true }, { 6089999, true }, { 608999, true },
{ 60899, true }, { 6089, true }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment