prime numbers program

• 11-18-2008, 02:34 AM
i contra i
prime numbers program
hey i need to create a program that will allow the user to input a number and determine if it is a prime number up to 500. It needs to be a loop that will ask the user for a number for how many times they want it to repeat. so far this is what i have but i know its not right... please help this is urgent

Code:

```import java.util.*; public class PrimeNumber   {     static Scanner console = new Scanner(System.in);     public static void main (String[] args)     {       int counter;       int guess;       int i = 2;       int b;             boolean done;       done = true;             System.out.print ("Enter an integer greater"                         + " than or equal to 0 and "                         + "less than 500: ");       guess = console.nextInt();       System.out.println();             System.out.println("Enter number of guesses you want");       counter = console.nextInt();             while (i < guess && counter > 0)       {         b = guess % i;         if (b == 0)         {           done = false;           counter--;         }         else if (done)         {           System.out.println("The number "                             + "you entered "                             + "is prime.");         }         else         {           System.out.println("The number "                             + "you entered "                             + "is not prime. \n"                             + "You have " + counter                             + " guesses left");         }     }     } }```
• 11-18-2008, 03:40 AM
emceenugget
There are many threads where people ask for help with prime numbers. I suggest looking at those.
• 11-18-2008, 03:46 AM
Fubarable
Quote:

so far this is what i have but i know its not right... please help this is urgent
How do you know that it isn't right? What does it do that is wrong? What doesn't it do that it should?
Quote:

You would be wise not to state this in your posts. Even if it is urgent to you, realize that it is not urgent to us. Many here take offense to this as it implies to them that a) the poster thinks that his post is more important than everyone else's, and b) that the poster wants to put pressure on the volunteers who come here to help on their own free time. Just a friendly word of advice.

Finally, where is the code for your prime test?
• 11-18-2008, 05:16 AM
i contra i
sorry about that... i know it doesn't work because the loop doesn't keep going. The program should loop however many times the user enters but after the first time it just stops and only displays if it isn't a prime number. Also my method to determine if the number is prime is also wrong because it only tests if its even.
• 11-18-2008, 06:15 AM
Write a method to determine if one number is prime.

private static boolean isPrime(int potentialPrime) {
if potentialPrime is even return false
if potentialPrime is less than 1 return false
loop through the numbers less than potentialPrime and greater than 1, if potentialPrime divides evenly by any number less than it, return false
else return true;
}

do something based on the return value of the isPrime method call, it seems like you have a good start
• 11-19-2008, 02:02 PM
Nicholas Jordan
Modulo operator is not sufficient to determine prime. Start with the fact that the factor cannot be larger than the square root of the sample, and the logic for beginner work often rests on whether sample / --counter shows a remainder. If it does not, then divides evenly and then sample cannot be a prime, consider 4,5,7,9,11 ( small values ) if 9 divides with zero remainder on some number - then just issue "not a prime" and break the loop.

Work the smaller numbers tediously by hand and you will discover the logic is backwards. There are many advanced techniques, finding beginner work on isPrime(int candidate) is not hard on the open wire.
• 11-19-2008, 03:18 PM
Isn't sufficient, or isn't efficient? I wrote a small program do to it using only modulo and it returns the first 10000 primes reliably. Seems like you are adding another restrict criteria to speed up the calculation.
• 01-15-2009, 04:56 AM
Nicholas Jordan
yes, you are correct...
Quote:

Isn't sufficient, or isn't efficient? I wrote a small program do to it using only modulo and it returns the first 10000 primes reliably. Seems like you are adding another restrict criteria to speed up the calculation.

Yes, really the modulo operator will pretty much to the job - it just looked to me like poster tried to do it all with an oversimplified, non-compis-computa ignoring degenerate cases such as 1 and abbreviating front of test for some simple obvious like three and five and the square root upper bound for test candidate.....

I don't know how to do any great deal of primatology, plus I could use some Fibbonaci soon on some fitting and bounds that is likely to come up soon in my work: sufficient || efficient was used interchange ( ably ) and loosely - a sub-type of the thinking that developes when doing this type of work, short words for a much bigger thought arena - spoken that way so as not to confuse poster.....
• 01-15-2009, 05:20 AM
mtyoung
Quote:

allow the user to input a number and determine if it is a prime number up to 500