1

I tried to write the BiggerThanPrime program which allows the user to give an input p and the program can find the next closest prime(n) to it such that the value of (n-p) is minimum.

Here is my code:

static boolean Prime (int n){
    boolean NotPrime = true;
    boolean result = true;
    for (int i=2; i< n; i++){
        NotPrime = (n % i != 0);
        result = (result && NotPrime);
    }
    return result;
}

//Using Betrand's Postulate which states that there always exists at least one prime p s.t.a< p <2a
public static void main(String[] args) {
    int p = Integer.parseInt(args[0]);
    int k = p+1;
    while( k > p && k< 2*p){
        if( Prime(k) == true){
            System.out.println("the next bigger prime than "+ p + " is "+ k);
        } else{
            k++;
        }
    }
}

But the while loop goes into infinite loop.

Thus the result is :

the next bigger prime than 20 is 23
the next bigger prime than 20 is 23
.
.
. 
(infinitely goes on) :(

What am I doing wrong?

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
meowthecat
  • 51
  • 8
  • Well, it doesn't change k or p... so `k>p && k<2*p` keeps being true, so the loop keeps looping. (That's how while loops work) – user253751 Apr 12 '15 at 11:23
  • Looking at your question, (n-p) is at a minimum when n = 2. I suspect that that was not the question you intended to answer. From you code, it appears to be, "What is the next prime greater than p". You "closest" may be below p. The closest prime to 19 is 17; the next greater prime after 19 is 23. Make sure you are answering the right question. – rossum Apr 13 '15 at 16:11

6 Answers6

2

You need to break the loop once you get the bigger prime number and also you need to increment k in each iteration as below:

while( k > p && k< 2*p){
    if(Prime(k)){
        System.out.println("the next bigger prime than "+ p + " is "+ k);
         break;
     }           
    k++; 
}

And also please note as Prime(k) returns a boolean value, It's not necessary again to compare it to true.

Iqbal S
  • 1,156
  • 10
  • 16
1

Once you find the next prime, you should break from the loop:

    while( k > p && k< 2*p){
        if( Prime(k) == true){
            System.out.println("the next bigger prime than "+ p + " is "+ k);
            break;
        } else{
            k++;
        }               
    } 
Eran
  • 387,369
  • 54
  • 702
  • 768
1

because you are not incrementing k at each iteration

Try this :

while( k > p && k< 2*p){
    if( Prime(k) == true){
        System.out.println("the next bigger prime than "+ p + " is "+ k);
     }           
      k++;
}
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
1
 while( k > p && k< 2*p){
            if( Prime(k) == true){
                System.out.println("the next bigger prime than "+ p + " is "+ k);
            } else{
                k++;
            }               
        }  

once Prime(k) returns true, you are not changing the value of k, thus loop continues.

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
1

when ( Prime(k) == true) it will never increment the value of k. So it will go to infinite loop.

you can change the lop in either of this ways

way 1:

 while( k > p && k< 2*p){
        if( Prime(k) == true){
            System.out.println("the next bigger prime than "+ p + " is "+ k);
        } 
        k++;
    }

Way 2 :

 while( k > p && k< 2*p){
        if( Prime(k) == true){
            System.out.println("the next bigger prime than "+ p + " is "+ k);
            Break;
        } else{
            k++;
        }
  }
Dileep
  • 5,362
  • 3
  • 22
  • 38
0
 import java.util.*;

 public class index_page
{

    public static void main(String[] args) {
       Scanner keyB = new Scanner(System.in);

     String code = keyB.next();
          while (!code.equals("01") ||  code.equals("02")  ||  code.equals("00")  || code.equals("03")  ||  code.equals("04") ||  code.equals("05")){//Checking for erros in code from user
          System.out.println("Invalid code!! Try again");
             code = keyB.next(); 
            }
    }
}
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74