Results 1 to 10 of 10
Thread: prime numbers program
- 11-18-2008, 01:34 AM #1
Member
- 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"); } } } }
- 11-18-2008, 02:40 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
There are many threads where people ask for help with prime numbers. I suggest looking at those.
-
How do you know that it isn't right? What does it do that is wrong? What doesn't it do that it should?so far this is what i have but i know its not right... please help this is urgent
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.please help this is urgent
Finally, where is the code for your prime test?Last edited by Fubarable; 11-18-2008 at 02:48 AM.
- 11-18-2008, 04:16 AM #4
Member
- 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.
- 11-18-2008, 05:15 AM #5
Member
- 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
- 11-19-2008, 01: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
- 11-19-2008, 02:18 PM #7
Member
- 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.
- 01-15-2009, 03: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, 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.....Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 01-15-2009, 04:20 AM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
why program then askallow the user to input a number and determine if it is a prime number up to 500
and may outputEnter number of guesses you want
computer generates prime number for user to guessThe 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; 01-15-2009 at 04:30 AM.
- 01-15-2009, 07:22 AM #10
Similar Threads
-
(Help) Quotient summation with prime numbers
By SapphireSpark in forum New To JavaReplies: 27Last Post: 10-24-2008, 08:28 AM -
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05-04-2008, 06:05 AM -
Computing prime numbers in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:39 PM -
Addition program that displays the sum of two numbers
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 08:46 PM -
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02-07-2008, 10:09 AM


LinkBack URL
About LinkBacks


Bookmarks