'Checking if a number is prime, not working by using extra isPrime flag [closed]
(1) Original question with problematic solution: This question is checking the number is prime or not.
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.println("Enter a prime number ( you think ) : ");
int num = input.nextInt();
isPrime = false;
for(int divisor = 2; divisor < num / 2; divisor++) {
if(num % divisor == 0){
isPrime = false;
}
isPrime = true;
}
if(isPrime) {
System.out.println("Prime");
}
else {
System.out.println("Not a prime");
}
}
(2) ###Updated: The main reason is not working is the flag caused the issue: isPrime = true;
isPrime = false;
for(int divisor = 2; divisor < num / 2; divisor++) {
if(num % divisor == 0)
{
isPrime = false;
}
isPrime = true; // May 2020: no matter what num, it will become true here.
}
(3) ###Updated: The working solution with a method:
class Prime {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a prime number ( you think ) : ");
int num = input.nextInt();
if(ifPrime(num)) {
System.out.println(num + " is a prime number");
}
else {
System.out.println(num + " is a NOT prime number");
}
}
private static boolean ifPrime(int num) {
for(int divisor = 2; divisor < num; divisor++) {
if( num != divisor && num % divisor == 0){
return false;
}
}
return true;
}
}
Solution 1:[1]
The main issue here is that you overwrite the value of isPrime in every iteration, so if the last divisor you check does not divide num, you interpret it as a prime.
A better approach would be to assume a number is a prime until proven otherwise (i.e., until you find a divisor for it). Once you've found such a divisor, you can break out of the loop - the number isn't a prime, and there's no reason to keep on checking it:
isPrime = true;
for(int divisor = 2; divisor <= num / 2; divisor++) {
if (num % divisor == 0) {
isPrime = false;
break; // num is not a prime, no reason to continue checking
}
}
Solution 2:[2]
In your code
for(int divisor = 2; divisor < num / 2; divisor++) {
if(num % divisor == 0)
{
isPrime = false;
}
isPrime = true;
}
If isPrime = false, then you make it true again!
You can consider this:
isPrime = true;
for(int divisor = 2; divisor < num / 2; divisor++) {
if(num % divisor == 0)
{
isPrime = false;
break;
}
}
Solution 3:[3]
Try with this:
public class prime{
public static void main (String args[]){
int n1=100, n2=1000;
int c=0;
for(int i=n1; i<=n2; i++)
if(isPrime(i)==true)
c++;
System.out.print(c);
}
public static boolean isPrime(int number)
{
for(int j=2; j<number; j++) //u go from number+1 to number to check
//if it can be divided by any other value.
if(number % j ==0) //if there is 1 other number that divides it then
//it returns false.
return false;
return true; //else it returns true.
}
}
Solution 4:[4]
public class PrimeNum {
private static boolean isPrime;
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.println("Enter a number ( you think ) : ");
String ch = input.next();
if (isNumeric(ch)) {
int num = Integer.parseInt(ch);
isPrime = true;
if (num > 1) {
for (int divisor = 2; divisor * divisor <= num; divisor++) {
if (num % divisor == 0) {
isPrime = false;
break;
}
}
} else {
isPrime = false;
}
if (isPrime) {
System.out.println("Prime");
} else {
System.out.println("Not a prime");
}
} else {
System.out.println("Should input a number");
}
}
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*|\\-[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | pjpj |
| Solution 3 | |
| Solution 4 | CapLuo |
