Results 1 to 10 of 10
  1. #1
    i contra i is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default 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");
            }
        }
        }
    }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    There are many threads where people ask for help with prime numbers. I suggest looking at those.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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?
    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.

    Finally, where is the code for your prime test?
    Last edited by Fubarable; 11-18-2008 at 03:48 AM.

  4. #4
    i contra i is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    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.

  5. #5
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    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

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    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

  7. #7
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    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.

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Arrow yes, you are correct...

    Quote Originally Posted by racerxadam View Post
    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.....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    allow the user to input a number and determine if it is a prime number up to 500
    why program then ask
    Enter number of guesses you want
    and may output
    The number you entered is not prime.
    You have " + counter + " guesses left
    computer generates prime number for user to guess
    or user enters a number and computer determines is prism or not?
    Last edited by mtyoung; 01-15-2009 at 05:30 AM.

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    You should break down the code and write a function to check if the number is prime or not. (google for some java prime function codes.) It should return a boolean value.

Similar Threads

  1. (Help) Quotient summation with prime numbers
    By SapphireSpark in forum New To Java
    Replies: 27
    Last Post: 10-24-2008, 09:28 AM
  2. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 07:05 AM
  3. Computing prime numbers in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:39 PM
  4. Replies: 0
    Last Post: 03-28-2008, 09:46 PM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •