Solving C Programming Problem: Finding Primes to 1000

  • Thread starter kbaumen
  • Start date
In summary, the conversation discusses a problem with a code that is meant to print out all primes to 1000 using the sieve method. The code only outputs a blank line and the person has spent hours trying to fix it. They receive suggestions to use printf statements to debug the code and eventually find the issue was a semi-colon misplaced in a for loop. They also mention a debugger called GDB, but it is suggested that one should be able to fix simple code issues without relying on a debugger.
  • #1
kbaumen
192
0
I do encounter a lot of problems while learning C. For example the code below, I can't understand why doesn't it work. It's meant to print out all primes to 1000 by using the sieve method. However, I only get a blank line. And I've spent about two hours making small modifications and repeatedly rereading the code with no result. Can anyone tell me why doesn't it work?

Here's the code:
Code:
#include <stdio.h>
#include <stdlib.h>

#define PRIME 1
#define NONPRIME 0

int numbers[1000];

void mark_multiples(int num)
{
    int a, j;  
    a = 0;
    j = 2;  

    while (a < 1000)
    {
        a = num * j;

        if (a >= 1000)
        {
            break;
        }

        numbers[a] = NONPRIME;
        j++;
    }
}

int get_next_prime(int num)
{
    int answer;

    answer = num + 1;

    while (numbers[answer] == NONPRIME)
    {
        answer++;
        if (answer == 1000)
        {
            break;
        }
    }

    return answer;
}

int main(void)
{
    int i, next_prime;
    
    numbers[0] = NONPRIME;
    numbers[1] = NONPRIME;

    for (i = 2; i < 1000; i++)
    {
        numbers[i] = PRIME;
    }

    next_prime = 2;    
    
    do
    {
        mark_multiples(next_prime);
        next_prime = get_next_prime(next_prime);
    }
    while(next_prime < 1000);

    for (i = 1; i < 1000; i++);
    {

        if (numbers[i - 1] == PRIME)
        {
            printf("%i ", i);
        }
    }
    
    printf("\n");

    return 0;
}

Thank you in advance.
 
Last edited:
Technology news on Phys.org
  • #2
Have you tried putting extra printf statements at key points in the code, to see what is actually happening to your variables? That often gives you clues as to what's wrong, faster than by just staring at the code. When doing this, it would probably help to reduce the upper limit temporarily from 1000 to perhaps 30, so the resulting output doesn't overwhelm you.

I haven't looked at your code in detail myself... I'm just offering a general suggestion.
 
  • #3
Well, thanks for the suggestions. I tried reducing the 1000 to 100. But that idea of more printf's sounds quite good

...

That worked. I changed the last loop from FOR to WHILE like this:

Code:
#include <stdio.h>
#include <stdlib.h>

#define PRIME 1
#define NONPRIME 0

int numbers[1000];

void mark_multiples(int num)
{
    int a, j;  
    a = 0;
    j = 2;  

    while (a < 1000)
    {
        a = num * j;

        if (a >= 1000)
        {
            break;
        }

        numbers[a] = NONPRIME;
        j++;
    }
}

int get_next_prime(int num)
{
    int answer;

    answer = num + 1;

    while (numbers[answer] == NONPRIME)
    {
        answer++;
        if (answer == 1000)
        {
            break;
        }
    }
    return answer;
}

int main(void)
{
    int i, next_prime;
    
    numbers[0] = NONPRIME;
    numbers[1] = NONPRIME;

    for (i = 2; i < 1000; i++)
    {
        numbers[i] = PRIME;
    }

    next_prime = 2;    
    
    do
    {
        mark_multiples(next_prime);
        next_prime = get_next_prime(next_prime);
    }
    while(next_prime < 1000);

    i = 0;
    
    while (i < 1000)
    {
        if (numbers[i] == PRIME)
        {
            printf("%i  ", i);
        }
        
        i++;
    }
    
    printf("\n");

    return 0;
}

Now it works perfectly. Thanks for the help.

However, I still have no idea why the previous one didn't work. All I changed is FOR to WHILE in main routine.
 
  • #4
You have a semi-colon after the for statement.
 
  • #5
t!m said:
You have a semi-colon after the for statement.

Thanks. That really was the problem. Got to be more careful.
 
  • #6
gdb?
 
  • #7
Texpie said:
gdb?

I'm sorry, what do you mean by that? What's gdb?
 
  • #8
kbaumen said:
I'm sorry, what do you mean by that? What's gdb?

GDB stands for GNU Debugger.

But I think it is not a good sugestion: before using any debugger, you should be able to correct a piece of code like this by yourself - alone.
 

Related to Solving C Programming Problem: Finding Primes to 1000

1. How do I find the prime numbers up to 1000 in C programming?

To find prime numbers up to 1000 in C programming, you can use a nested loop and the modulus operator to check if a number is divisible by any number other than 1 and itself. If not, then it is a prime number. You can also use a sieve algorithm to find prime numbers efficiently.

2. Can I use a built-in function in C to find prime numbers?

No, C does not have a built-in function to find prime numbers. You will have to write your own code or use a library that provides a function for finding prime numbers.

3. How do I optimize my code for finding prime numbers in C?

One way to optimize your code is to use the Sieve of Eratosthenes algorithm, which eliminates the need to check all the numbers up to the given limit. You can also use the concept of prime factorization to find prime numbers efficiently.

4. Can I use recursion to find prime numbers in C?

Yes, you can use recursion to find prime numbers in C. However, it is not the most efficient approach, and it may lead to stack overflow for large values. It is better to use an iterative approach for finding prime numbers in C.

5. How do prime numbers relate to real-world applications?

Prime numbers have various applications in real-world scenarios, such as cryptography, data encryption, and coding theory. They also play a crucial role in algorithms for optimizing computer programs and in generating random numbers.

Similar threads

  • Programming and Computer Science
Replies
22
Views
896
  • Programming and Computer Science
Replies
9
Views
833
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
760
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top