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

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

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

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

    classical error that all C coders make before...
  4. #3
  5. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    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.
  6. #4
  7. 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.??
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    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.
  10. #6
  11. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    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);
    }
  12. #7
  13. 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.
  14. #8
  15. 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);
    }
  16. #9
  17. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    If your second for statement was:

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

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

IMN logo majestic logo threadwatch logo seochat tools logo