It is necessary to determine if the number entered by the user is prime or not.
This is what I have, but it always results in not being prime.
public static void primeNumber(int num) {
boolean prime = true;
for(int i = 2; i < num; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
if (prime)
View.input("The number is prime.");
else
View.input("The number isn't prime.");
}
The definition of a prime number is a number that can only be divided by 1 and itself .
Therefore you can use this method:
You can find more optimized methods in this article by Oscar Sanchez
This is the most efficient method described:
There was a very similar answer from @ArtEze recently, although it included more cases.
This was the solution for the prime number:
In addition, one that @AlexisRodriguez contributed below these comments (although it was deleted because it was not understood as a comment).