The Artima Developer Community
Sponsored Link

Java Answers Forum
URGENT - PRIME CHECKER ??

4 replies on 1 page. Most recent reply: Apr 12, 2004 12:26 AM by Martijn Veening

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
nicola

Posts: 3
Nickname: nicola
Registered: Apr, 2004

URGENT - PRIME CHECKER ?? Posted: Apr 10, 2004 2:55 AM
Reply to this message Reply
Advertisement
Hello, I am new to all this Java and have been desperately trying to work out how to write a primechecker program.

I need to accept a positive integer as input and determine whether the number is prime by using the formula:

An integer number n is prime if it is not evenly divisible by any integer number k(starting at 2), such that k[/] squared <= n.

The program should accept only positive numbers, and an error message should display if it is negative that is entered. The prompts should continue util the number entered is 0.

I would appreciate anyones help. I really want to understand how this works. Thank You


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: URGENT - PRIME CHECKER ?? Posted: Apr 10, 2004 12:36 PM
Reply to this message Reply
Have you written any code or are you expecting us to do your homework for you? I don't mind helping you find errors in your code or your logic, but I'm not doing anyones homework for them.

nicola

Posts: 3
Nickname: nicola
Registered: Apr, 2004

Re: URGENT - PRIME CHECKER ?? Posted: Apr 11, 2004 6:36 AM
Reply to this message Reply
I am sorry if it came across that way. I didn't mean to have it all done for me - how dreadful!! I would never learn like that.

I do have code I have done for it. I have been having difficulty trying to interpret where I use the do while code and wher I use the for loops. I keep trying different combinations and just can't seem to make it all happen. I have read all my notes and reread them a zillion times but none of the information provides on how to do it when you need to use a combination of them all.

Just some guidance on what I have already done would be appreciated. Here is the code:

 import java.io.*;
class PrimeChecker
{
   public static void main(String[] args) throws IOException
   {
   
   BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
   int num=0, i=2, sqrtnum;
   
   
   do
   {
      System.out.println("Enter a number:");
      num = Integer.parseInt(stdin.readLine());
      while (num!=0)                    
        {
          sqrtnum = (int)Math.sqrt(num);
      while (i <= sqrtnum);
      if (num%i == 0)
      i++;
      if (i == sqrtnum+1)
      
    System.out.println(num + " is a prime number");
   else
   System.out.println(num + "is not a prime number");
      }
      
      break;
      }
       while (num >= 0);
       System.out.println("You entered a negative number");
      
             
   }
   } 

nicola

Posts: 3
Nickname: nicola
Registered: Apr, 2004

I HAVE DONE THE WORK - NEED GUIDANCE !!! Posted: Apr 11, 2004 11:04 PM
Reply to this message Reply
I have gotten this far with my code. I have researched this all day and have spent over 10 hours on it. I have got the program as far as providing the right answer as to whether it is prime or not and if the number entered is negative a warning message appears and asks for the number to be re-entered.

2 things I have been unable to accomplish.
1: for the program to continually loop and ask for a number to be entered, providing an answer each time until a zero is entered.
and
2: for the program to exit when zero is typed in.

please help me, I obviously have some do, whiles, ifs, and fors wrong!! I have tried so many different combinations.

Here is the code I have so far:

import java.io.*;
class PrimeChecker
{
   public static void main(String[] args) throws IOException
   {
   
      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
      int num = 0;
      int i=2;
      int sqrtnum; 
      
      while (num <=0)          
      {
      
      System.out.println("Enter a number:");
         num = Integer.parseInt(stdin.readLine());
         while (num!=0)
         {
            if (num < 0)
            System.out.println("You entered a negative number");
            break;
         }
      }      
      sqrtnum = (int)Math.sqrt(num);
      while (i <= sqrtnum)
         {
            if (num%i == 0)
            break;
            i++;
        }
      
      if (i == sqrtnum+1)
        System.out.println(num + " is a prime number");
      else
        System.out.println(num + " is not a prime number");
   }
}

Martijn Veening

Posts: 1
Nickname: martijn
Registered: Apr, 2004

Re: I HAVE DONE THE WORK - NEED GUIDANCE !!! Posted: Apr 12, 2004 12:26 AM
Reply to this message Reply
You missed something trivial indeed.
Your brackets don't include the right stuff. The closing bracket right before sqrtnum=(int)Math.sqrt(num) line should not be there. You probably missed that because you have a tab on the line num=Integer.parseInt(stdin.readline()), which does not belong there.
So delete that ending-bracket, and add it at the end of the while-loop, just before the last two ending-brackets.
Furthermore, you can optimalize the while loop by rewriting it to
while (i<sqrtnum && num%i!=0) i++
The while-loop then ends as soon as it has found a divisor, your algorithm goes on after that until sqrtnum has been reached.
Also, first try checking divisibility by 2, and then increment i from 3 with steps of 2 (i+=2 instead of i++).
This excludes the unnescessary checks of 4,6,8 etc if 2 was not a divisor (if num%2!=0, num%4!=0).
Hope this helped you.

Flat View: This topic has 4 replies on 1 page
Topic: 'cannot resolve symbol' - why? Previous Topic   Next Topic Topic: Help with toHexString

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use