### Thread: listing Prime numbers in C

1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Location
Gold Coast, Australia
Posts
5
Rep Power
0

#### 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);
}
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Location
Singapore
Posts
31
Rep Power
15
change ur if statement to:
if (prime[i] == true){

classical error that all C coders make before...
3. 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.
4. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Location
Gold Coast, Australia
Posts
5
Rep Power
0
Thanks Siddy, but that didnt make any diference.??
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
138
Rep Power
15
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.
6. 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);
}```
7. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Posts
14
Rep Power
0

#### 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.
8. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Location
Gold Coast, Australia
Posts
5
Rep Power
0
:)
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);
}
9. If your second for statement was:

for (j = 2; j < i; j++)

Then you wouldn't need the "(j < i)" in your if statement.