  aleshik1
join:2004-07-10 Newark, NJ
·Optimum Online
1 edit | please tell me why my c++ app doesnt work?
#include using namespace std; int main(){
int number; int factor; int remainder; int test;
for (number=4;;number++)
{ for (factor=2;factornumber;factor++) { int remainder=number/factor; if (int remainder=0){int test=0;} for(;test=0;number++){} }
if(test!=0){cout number;} }
return 0; }
I want it to display prime numbers from 4 and above.By tweaking it I can either get it to display all numbers or none at all.I cant seem to get it right.  |
|
  PToN
join:2001-10-04 Houston, TX
| your for loops are all wrong. Check your book and re-read the looping chapter. Or make a google search so you can understand it.
It should look something like:
|
|
  Steve I'm a PC, so shut up Consultant join:2001-03-10 Yorba Linda, CA | reply to aleshik1 You'll also need to use the [code] tags so we can see your code; many of us (including me) won't really even look at stuff that's essentially unformatted. |
|
  cowboyro
join:2000-10-11 Shelton, CT | reply to PToN condition doens't always have to exist in his case since it's for all numbers - so basically an infinite loop  |
|
  cowboyro
join:2000-10-11 Shelton, CT
·AT&T U-Verse
·Comcast
·Optimum Voice
| reply to aleshik1 this shoud give a hint - haven't checked it though
|
|
  cowboyro
join:2000-10-11 Shelton, CT
·AT&T U-Verse
·Comcast
·Optimum Voice
| reply to aleshik1 Anyway this is the low level, brute force approach... However it can be refined since: -all even numbers greater than 2 are NOT prime as they can be divided by 2, so only odd numbers should be tested -divisors that are even numbers should not be tested as they are multiple of 2, and you know the odd numbers that you are testing cannot be divided by 2 with a reminder of 0  Therefore you can tweak the code to start with number 3 for testing and increment by 2, and for divisor start with number 3 and increment by 2. |
|
  PToN
join:2001-10-04 Houston, TX
| reply to cowboyro quote: from 4 and above
I missed the "above" part :) , even then he could still used a condition
But i see what you saying :p |
|