This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes primality formula Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "primality formula" Watch "primality formula" New topic
Author

primality formula

sam mukhtar
Greenhorn

Joined: Mar 21, 2004
Posts: 12
hi guys, i have a huge java assignment that i have been working on for weeks, part of the assignment is asking about primarlity formula, here is the exact transcript:
"An integer number n is prime if it is not evenly divisible by any integer number
k (starting at 2), such that k 2 <= n. For example, to check whether or not the
number 29 was prime, the algorithm would check if any of the numbers
between 2 and 5 evenly divided 29. Since none do, it can be concluded that
29 is a prime number."
can someone please explain to me in simple terms what this formula should be like or what it means or how it works..any ideas r welcomed
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
I believe this is the answer to your assignment. Prime Numbers


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
sam mukhtar
Greenhorn

Joined: Mar 21, 2004
Posts: 12
o.k..i'v come up with this, but somehow its not working probably..can someone tell me why:
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ April 14, 2004: Message edited by: Dirk Schreckmann ]
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Originally posted by Cheng Wei Lee:
I believe this is the answer to your assignment. Prime Numbers

Cheng, you may want to read the On responding to (home)work inquiries topic recently posted by one of the sheriffs.
sam mukhtar
Greenhorn

Joined: Mar 21, 2004
Posts: 12
Eugene...he didn't give me the solution to the home work..just some info to help me understand it..after i read the info i come up with my own code but i have a problem with it which i need some help on..that is it..so please can u see anything wrong with the code because it is not doing what is supposed to do
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
First, I would like to mention that it will help us be able to answer your questions more clearly if you can give specifics about what happens when you run your program. Just telling us that "it doesn't work" isn't very helpful.
To answer your question, I'd like to first ask if it compiles. If not, you should post compiler errors so we can help you fix those. If it does compile, you should describe what your program does and how this differs from what you expect.
With that said, I see two problems with your code. Let's take a look at these lines:

The first thing I notice is the extraneous semicolon after the while statement. The while statement only executes the statement immediately after the condition. In this case, the semicolon meand "do nothing." This will cause an infinite loop since the value of i will never change.
To fix this, you need to remove the semicolon and put curly braces {} around the statements that you want repeated in the while loop. Also, since you know exactly how many times the loop should repeat, you should consider using a for loop rather than a while loop. Typically while loops are used when you don't know the number of times to repeat but there is some flag or condition that must be met to continue the repition.
Next, the second and third lines say that if the number is divisible by then increment i. In other words, i doesn't change if num is NOT divisible by i. Even if you fix the problem with the while statement mentioned previously, this if statement won't do what you need exactly.
I don't have any suggestions to fix this last problem because I'm not entirely sure what you intended. One thing that might help is if you write out a description of your algorithm in your native language. Use the concepts of looping (with while or for) and selection (with if), but don't worry about the Java syntax too much. Learning to write pseudocode for an algorithm is an invaluable tool, imho. From there, you can work on translating it into Java code.
I think I'll just leave it at that. Please come back with some more questions as you work on this.
HTH
Layne
p.s. If you wish to keep the formatting for your code (which I strongly suggest), there is a button below the reply box that will add [ CODE ] tags to your message. Just paste your code in between the tags. This will help us be able to read your code much more easily.


Java API Documentation
The Java Tutorial
sam mukhtar
Greenhorn

Joined: Mar 21, 2004
Posts: 12
layne .the code is compiling fine however when i moved the semicolon, it became an infinte loop for the answer..i tried to put curly brackets..it came up with errors pointing at the else statement:
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");

}
}
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Okay, let's work on that bit of code. Here it is with formatting:

Notice how I use indenting to indicate what statements are "inside" of the control structures. This is a common practice and helps you easily see the structure of a program. However, I doubt that this is the structure you meant to have. It is simply the structure I have implied from your use of braces.
Notice that the else is at the same indentation level as the while. This is the reason your compiler is complaining: it can't find an if statement that connects with the else part. I suggest that you use some indentation as I have illustrated above to help figure out how the code should be structured. Every time an if statement or while loop starts, indent the next line of code. Every statement that belongs "inside" the if or while should be indented. As soon as you encounter a statement that doesn't belong "inside", then go back out one level of indentation. At this point, don't worry about curly braces. Myself or someone else can help you with that once you figure out which statements are "inside" the control structures (i.e. if or while).
I'm being somewhat vague on purpose. Hopefully I have explained what I mean well enough for you to make some progress.
Good luck, and let us know what you get stuck on next.
Layne
p.s. Please use the UBB Code [ CODE ] tags when you post your code. This will preserve the indentation. Click on the link to learn how.
[ April 13, 2004: Message edited by: Layne Lund ]
[ April 13, 2004: Message edited by: Layne Lund ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: primality formula