1 2 Previous Next 16 Replies Latest reply: Sep 29, 2008 3:25 PM by 843785

# Need help with finding prime numbers

I have seen this question asked several times so i apologize for asking once again but I am very new to programming and could use some help. I know the way I have the extra if statements is not at all efficient but I cannot seem to figure out how to get a nested for statement with a divisor to work. The error I'm getting with this current code is that the second for statement needs to be boolean.
``````/*
*
*
*/

/**
*
* @author Owner
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int primecount; //running total of prime numbers
int incrementer; //increment numbers
int sqrtInc; //square root of current increment
int divisor;

//set variables to 0
primecount =0;
incrementer=3;
sqrtInc = (int)Math.sqrt(incrementer);
divisor = 0;

//print 2
System.out.println("2");

for(incrementer = 3; incrementer < 10000; incrementer = incrementer + 1 )

{

if (incrementer % 2 != 0)
if (sqrtInc % 3 != 0)
if (sqrtInc % 5 != 0)
if (sqrtInc % 7 != 0)
if (sqrtInc % 9 != 0)
if (sqrtInc % 11 != 0)
for (divisor = 3; divisor = incrementer; divisor = divisor +1)
if ( incrementer % divisor !=0)

if (incrementer % (Math.sqrt(incrementer)) != 0)
System.out.println(incrementer);
}

}

}``````
• ###### 1. Re: Need help with finding prime numbers
1) Describe in English and/or pseudocode the algorithm you're trying to implement. You can do this with zero knowledge of Java.

2) What do you mean by "the second for statement needs to be boolean"?
• ###### 2. Re: Need help with finding prime numbers
1. I need to have a program that lists every prime number up to 10,000. The first for statement increases the number (incrementer) and writes numbers that are prime based upon the criteria I have listed but as it is now it writes many numbers that are not prime. I want the second for statement to divide the incrementer by a divisor that increases from 3 to the incrementer.

2. I use Netbeans 6.1, and the second for statement is flagged as an error. It says incompatible types > found: int > required: boolean

Thanks for looking at this.
• ###### 3. Re: Need help with finding prime numbers
aeakers wrote:
1. I need to have a program that lists every prime number up to 10,000. The first for statement increases the number (incrementer) and writes numbers that are prime based upon the criteria I have listed but as it is now it writes many numbers that are not prime. I want the second for statement to divide the incrementer by a divisor that increases from 3 to the incrementer.
This does not describe the algorithm at all, and it does use Java. Both are the opposite of what I suggested.
2. I use Netbeans 6.1, and the second for statement is flagged as an error. It says incompatible types > found: int > required: boolean
``    for (divisor = 3; divisor = incrementer; divisor = divisor +1) ``
What is this for loop supposed to do? The second piece has to be a boolean expression, like it is in your first one--you have to be testing something. What do you think you're testing here?
• ###### 4. Re: Need help with finding prime numbers
2. I use Netbeans 6.1, and the second for statement is flagged as an error. It says incompatible types > found: int > required: boolean
The error is in the stop criterium of your for loop: the result of the statement isn't a boolean. Use == in stead of =
• ###### 5. Re: Need help with finding prime numbers
jverd wrote:
1. I need to have a program that lists every prime number up to 10,000. The first for statement increases the number (incrementer) and writes numbers that are prime based upon the criteria I have listed but as it is now it writes many numbers that are not prime. I want the second for statement to divide the incrementer by a divisor that increases from 3 to the incrementer.
This does not describe the algorithm at all, and it does use Java. Both are the opposite of what I suggested.
2. I use Netbeans 6.1, and the second for statement is flagged as an error. It says incompatible types > found: int > required: boolean
``    for (divisor = 3; divisor = incrementer; divisor = divisor +1) ``
What is this for loop supposed to do? The second piece has to be a boolean expression, like it is in your first one--you have to be testing something. What do you think you're testing here?
This does not describe the algorithm at all, and it does use Java. Both are the opposite of what I suggested.

2. I use Netbeans 6.1, and the second for statement is flagged as an error. It says incompatible types > found: int > required: boolean

for (divisor = 3; divisor = incrementer; divisor = divisor +1)

What is this for loop supposed to do? The second piece has to be a boolean expression, like it is in your first one--you have to be testing something. What do you think you're testing here? >

I'm testing the modulus to not equal 0 when doing the various calculations (incrementer /2, sqrt of the incremented divided by several odd numbers - which ideally i'd like replaced)

The guy below helped figure out the Boolean issue - I just needed the double equal sign. So now, the program works - but not as intended. It writes a 2 and nothing else. Any ideas?
• ###### 6. Re: Need help with finding prime numbers
aeakers wrote:
Any ideas?
See suggestion 1 in reply 1.
• ###### 7. Re: Need help with finding prime numbers
I thought I had described it. I appreciate the help but it seems pretty clear to me what this program is supposed to do. And especially in the followup posts.

Program should list every prime number that is less than 10000.
I have it to check if the modulus is not zero for the following conditions:
-divided by 2
-square root divided by 3
-square root divided by 5
-square root divided by 7
-square root divided by 9
-square root divided by 11

Then check the modulus is not zero for the following
number divided by a second number that increases up to the first number

I am really trying to get you the information you need but I am not at all familiar with this.
• ###### 8. Re: Need help with finding prime numbers
aeakers wrote:
I am really trying to get you the information you need but I am not at all familiar with this.
You are missing the point. Jverd does not need to know how to determine if a number is prime or not. I'm sure he knows several algorithms to do this. The whole point is for you to see if you can describe it in English (or whatever your native language is) without using any Java (programming) terms. Once you can do that, it should me a simple matter of converting that into code.
• ###### 9. Re: Need help with finding prime numbers
I know how to tell if a number is prime. I do not know how in java to use nested for loops to decrement the divisor as I test 'i/d !=0'
• ###### 10. Re: Need help with finding prime numbers
If you had done as suggested and describe how to do this in plain ol' English then you will realise you do not need nested loops.
• ###### 11. Re: Need help with finding prime numbers
check this out brother,

• ###### 12. Re: Need help with finding prime numbers
aeakers wrote:
I thought I had described it. I appreciate the help but it seems pretty clear to me what this program is supposed to do.
Yes, what it's supposed to do is clear: It's supposed to find some prime numbers.

What's not clear, and what I'm asking about, is what algorithm you're trying to use. There's more than one way to find primes, and they all start with no regard whatsoever or any programming language. Then they get implemented in a programming language. It's not clear at all from that mess of code what algorithm you're trying to implement.
And especially in the followup posts.
Nope. Nothing was clarified there.
Program should list every prime number that is less than 10000.
I have it to check if the modulus is not zero for the following conditions:
-divided by 2
-square root divided by 3
-square root divided by 5
-square root divided by 7
-square root divided by 9
-square root divided by 11
Why those particular numbers?
Then check the modulus is not zero for the following
number divided by a second number that increases up to the first number
I think I kind of get what you're saying here, but I don't think you understand it well enough to translate it to Java.
I am really trying to get you the information you need but I am not at all familiar with this.
The point here is that programming is as much about clarity and specificity of thought and communication as it is about the mechanics of the language. If you can't clearly and precisely express what you're trying to do--without regard to any programming language--then you won't be able to program it.

At the very least, if you can't find a way to express your algorithm clearly and precisely, then you ought to put print statements in at each step of your program so you can see exactly what's happening each step of the way, compare that to what you think should be happening, and then find out where you're going wrong.

Also, as a point of best practice, you should alaways use braces for if, for, etc., even when they're not strictly necessary.
``````// bad
if (...)
singleStatement();

// good
if (...) {
singleStatement();
}``````
• ###### 13. Re: Need help with finding prime numbers
aeakers wrote:
I know how to tell if a number is prime.
But can you describe it, clearly and precisely.
I do not know how in java to use nested for loops to decrement the divisor as I test 'i/d !=0'
If you can describe it clearly and precisely in English, and you know the basics of Java syntax, then you can do it.
• ###### 14. Re: Need help with finding prime numbers
Thanks for the help. I am going to start from scratch and attack it from a different angle as my current direction does not seem to be the right way - or at least a way I can figure out.
1 2 Previous Next