-2
public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
    int N = 0;
    do {
        System.out.println("How long should we search for primes? Until N=: ");
        N = scan.nextInt();
    } while (N <= 2); // gets the amount of prime numbers there are that go up to that number 
    //example, if user enters 20, the output will be that there are 8 prime numbers 

    boolean[] prime = new boolean[N +2];
    for (int i = 2; i <= N; i++) {
        prime[i] = true; //makes all values in the array true 
    }
    for (int i = 2; i * i <= N; i++) {
        if (prime[i]) {
            for (int z = i; z * i <= N; z++) {
                prime[i * z] = false; // makes the non prime numbers in the array false 
                int newCheck = 0;
                do {
                    System.out.println("Enter a number to see if it is prime");
                    int go = 0;
                    newCheck = scan.nextInt(); //here's where i need help 
                    } while (newCheck <= 1 || newCheck > N);
                    if(prime[newCheck]){ // if the number entered is true in the array, it is prime
                        System.out.println("It is prime");
                        int mPrime=(int)((Math.log(newCheck))/(Math.log(2)))-1;
                      if (prime[mPrime]){//ignore this, its for another part i need to do
                          System.out.println(""+newCheck+ "is a merseinne prime number! It equals 2^"+mPrime+ " -1");
                    }  
                    }

                    else if (prime[newCheck]==prime[i*z]){ //if the number is equal to false in the array,
                        //it is not prime 
                        System.out.println("It is not prime");
                    }
                    if(newCheck==0){
                        break;
                    }

            }
        }
    }
    int counterPrime = 0;
    for (int i = 2; i <= N; i++) {
        if (prime[i]) {
            counterPrime++;
        }
    }

    System.out.println("The number of primes less than or equal to " + N + " is " + counterPrime);

}

I need help with trying to output to the user that the number they entered is prime. So far this example only works for some numbers. The program thinks 14 is prime, 12 is prime, 25 is prime, 35 is prime,36 is prime,39 is prime,and that 34 is prime. It gets some numbers right though. It knows 8, 10, 12,18 and some other numbers are not prime.

Castaray
  • 1
  • 1
  • 2
    Sorry, questions consisting of a code dump and "please debug my code for me" are considered off-topic. You should start by stepping through the code one line at a time in your IDE debugger. You will likely find the problem very quickly. If you do not, then identify the line where the program's behavior doesn't match your expectation and ask a question about that situation. – Jim Garrison Apr 14 '16 at 02:15
  • Possible duplicate of [Find a prime number?](http://stackoverflow.com/questions/1583413/find-a-prime-number) – Vikrant Kashyap Apr 14 '16 at 04:51

1 Answers1

-1

Here is a little method to help you along:

public static boolean isPrime(final int number) {
    int temp;
    boolean isPrim = true;
    for(int i=2; i <= number / 2; i++) { 
            temp = number%i;
        if(temp==0) { isPrim = false; break; }
    }
    return isPrim;
}

Hope this helps.

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
  • I didn't downvote. I don't know who did. Thanks for the help. – Castaray Apr 15 '16 at 00:25
  • Castaray, there are more efficient versions of `isPrime()` than this. I suspect that DevilsHnd wants you to do some more learning and research those more efficient methods for yourself. Hint: how many even prime numbers are there? – rossum Apr 15 '16 at 16:39