Results 1 to 10 of 10
Thread: prime numbers program
 11182008, 02:34 AM #1Member
 Join Date
 Nov 2008
 Posts
 2
 Rep Power
 0
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
PHP 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"); } } } }
 11182008, 03:40 AM #2Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 9
There are many threads where people ask for help with prime numbers. I suggest looking at those.
 so far this is what i have but i know its not right... please help this is urgent
please help this is urgent
Finally, where is the code for your prime test?Last edited by Fubarable; 11182008 at 03:48 AM.
 11182008, 05:16 AM #4Member
 Join Date
 Nov 2008
 Posts
 2
 Rep Power
 0
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.
 11182008, 06:15 AM #5Member
 Join Date
 Jul 2008
 Posts
 68
 Rep Power
 0
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
 11192008, 02:02 PM #6
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.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
 11192008, 03:18 PM #7Member
 Join Date
 Jul 2008
 Posts
 68
 Rep Power
 0
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.
 01152009, 04:56 AM #8
yes, you are correct...
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, noncompiscomputa 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 subtype 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.....Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
 01152009, 05:20 AM #9Senior Member
 Join Date
 Dec 2008
 Location
 Hong Kong
 Posts
 473
 Rep Power
 8
allow the user to input a number and determine if it is a prime number up to 500
Enter number of guesses you want
The number you entered is not prime.
You have " + counter + " guesses left
or user enters a number and computer determines is prism or not?Last edited by mtyoung; 01152009 at 05:30 AM.
 01152009, 08:22 AM #10
Similar Threads

(Help) Quotient summation with prime numbers
By SapphireSpark in forum New To JavaReplies: 27Last Post: 10242008, 08:28 AM 
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05042008, 06:05 AM 
Computing prime numbers in Java
By Java Tip in forum java.langReplies: 0Last Post: 04122008, 08:39 PM 
Addition program that displays the sum of two numbers
By Java Tip in forum Java TipReplies: 0Last Post: 03282008, 09:46 PM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 11:09 AM
Bookmarks