August 12th, 2003, 06:09 AM

listing Prime numbers in C
i am trying to generate a list of prime numbers in C...
following is what I've got. I can only make it print all numbers, where have i gone wrong???
#include <stdio.h>
#define PRIME_MAX 10000
#define NUM_MAX 200
int main() {
int i, j;
bool prime[PRIME_MAX];
int num[NUM_MAX];
int counter;
prime[PRIME_MAX] = i;
num[NUM_MAX] = j;
printf("\n Prime numbers from 1 to %d\n", PRIME_MAX);
printf("\n");
counter = 0;
for (i = 1; i < PRIME_MAX; i++) {
prime[i] = true;
for (j = 2; j < NUM_MAX; j++) {
if (prime[i] % j == 0) {
prime[i] = false;
break;
}
}
if (prime[i] = true) {
printf("%6d%c", i, (i + 1) % 10 == 0 ? '\n' : ' ');
counter = counter + 1;
}
}
printf("\n");
printf("Total prime numbers less than %d: %d\n", PRIME_MAX, counter);
}
August 12th, 2003, 06:43 AM

change ur if statement to:
if (prime[i] == true){
classical error that all C coders make before...
August 12th, 2003, 06:45 AM

A few problems in the code
1) Your final test if (prime[i] = true) will print out all values, as it does an assignment to true. You need a second equals sign.
2) Your test in the second loop if (prime[i] % j == 0) doesn't do what you think. prime[i] will always have a value of 1 or 0 (true or false). The test does not find primes. I suspect you intended something like if (prime[i] && (j % i == 0)) ....
You might try doing a google search for "seive of erastothenes"; that is the technique you're trying to use.
August 12th, 2003, 06:49 AM

Thanks Siddy, but that didnt make any diference.??
August 12th, 2003, 09:23 AM

I think the problem is when you are assigning prime[i] = true;
the array of prime is bool and u r trying to perform a mod operation on it. So each time u are going to divide the number 1 so each time you will be setting the flag to false and all the numbers are printed.
August 12th, 2003, 12:18 PM

Here's some changes:
Code:
#include <stdio.h>
#define PRIME_MAX 10000
//#define NUM_MAX 200 // unused
int main() {
int i, j;
bool prime[PRIME_MAX];
//int num[NUM_MAX]; // unused
int counter;
//prime[PRIME_MAX] = i;
//num[NUM_MAX] = j;
// since i and j aren't set yet, these don't do anything
printf("\n Prime numbers from 1 to %d\n", PRIME_MAX);
printf("\n");
counter = 0;
for (i = 1; i < PRIME_MAX; i++) {
prime[i] = true;
for (j = 2; j < i; j++) {
if (i % j == 0) {
prime[i] = false;
break;
}
}
if (prime[i] == true) { //common mistake
printf("%6d%c", i, (i + 1) % 10 == 0 ? '\n' : ' ');
counter = counter + 1;
}
}
printf("\n");
printf("Total prime numbers less than %d: %d\n", PRIME_MAX, counter);
}
August 12th, 2003, 12:45 PM

been there... done that...
Here's a tip:
You really shouldn't post the same question more than once... especially if you change the subject... all on the same day. Give people time to respond.
August 12th, 2003, 09:59 PM

:)
Ok, thanks for your help.. Combining some of the answers & thinking a bit about it I managed to fix it.... this is the answer if you're interested..
#include <stdio.h>
#define PRIME_MAX 10000
#define NUM_MAX 9
int main() {
int i;
int j;
bool prime[PRIME_MAX];
int num[NUM_MAX];
int counter;
prime[PRIME_MAX] = i;
num[NUM_MAX] = j;
printf("\n Prime numbers from 1 to %d\n", PRIME_MAX);
printf("\n");
counter = 0;
for (i = 0; i < PRIME_MAX; i++) {
prime[i] = true;
for (j = 2; j < NUM_MAX; j++) {
if ((j < i) && (i % j == 0)) {
prime[i] = false;
}
}
if (prime[i] == true) {
printf("%6d%c", i, (counter + 1) % 10 == 0 ? '\n' : ' ');
counter = counter + 1;
}
}
printf("\n");
printf(" Total prime numbers less than %d: %d\n", PRIME_MAX, counter);
}
August 13th, 2003, 11:33 AM

If your second for statement was:
for (j = 2; j < i; j++)
Then you wouldn't need the "(j < i)" in your if statement.