简体   繁体   中英

Displaying primes from 2-10,000 in C

I'm struggling with this (optional) problem my professor recommended I try. Basically, my task is to write a program which displays all prime integers from 2-10,000 using my own user-defined function to determine prime-ness. It sounded simple enough but I'm having major difficulties debugging my program. For some reason, my code only displays 2 and 3 before ending.

#include<stdio.h>
//function declaration
int prime(int);
//main body
int main(void)
{
    int x=2, y;
    for (x=2;x<=30;x++)
    {
        y=prime(x);
        if (y!=0)
            printf("%d\n", x);
    }
    getchar();
    return(0);
}
//function definition
int prime(int x)
{
    int y;
    for (y=2; y<=(int)sqrt(x); ++y)
    {
        if (x%y==0)
           return 0;
    }
    if (y==(int)sqrt(x))
       return 1;
}

Instead of returning 1 if x is prime, my prime checking function seems to return a random large number (2686xxx) but that shouldn't be an issue because all primes return 0. If I run something like:

if (y==0)
    printf("%d\n", x);

I see a list of all non prime numbers. If I run something like:

printf("%d    %d\n", x, y);

I see a list of all integers from 2-10,000 and the result of my prime checking function (0 for non-primes, 2686xxx for primes).

Why doesn't the opposite (y!=0) display a list of prime numbers? What is causing my code to stop after just displaying 2 and 3? Why is my prime function returning a weird integer instead of 1? Finally, I'm still a beginner but how can I write better code in general? I don't think I'm breaking any of the standard accepted practices but how can I make my code more clean or efficient?

Thanks in advance for the help!

Your loop continues if y==(int)sqrt(x) . So when it finishes , they're not equal. What you wanted is:

if (y>=(int)sqrt(x))
   return 1;

But this is not needed at all. Just return 1; is sufficient. You've already returned zero if the number isn't prime.

If you wanted only a single return statement:

int prime(int x)
{
    bool isPrime = true;
    int y;
    for (y=2; y<=(int)sqrt(x); ++y)
    {
        if (x%y==0)
        {
            isPrime = false;
            break;
        }
    }
    return isPrime;
}

Don't use the sqrt() function. In mathematics if you have 'x = sqrt(y)'. If you square both sides you will get something like this 'x * x = y'. This expression in c is tremendously faster than the sqrt function. Thus instead of doing:

y <= (int)sqrt(x)

Have you for loop guard be something like this:

y * y <= x

Here is a running example of your problem:

Primes 2 -> 10000

At the end of your prime function just return 1. If it wasn't prime it would have returned 0 earlier. Right?

As it is you've made a function which sometimes returns nothing at all. Which means that it returns whatever random value happens to be in the register.

You can use sieve of eratosthenes or sieve of atkin to mark all the prime numbers in an array and then display the prime numbers. It will be time efficient although it incurs some space complexity.

eg if you want to display prime numbers from 1 to 10

Leave of 1. Its not a prime. Its neither prime nor composite.

So start from 2.

consider this array of size 10 = 2 3 4 5 6 7 8 9 10

Traverse from 2. If an element is not highlighted highlight all its multiples.

ie for 2 highlight its multiples 4 6 8 10

==> 2 3 4 5 6 7 8 9 10

For 3 do the same

==> 2 3 4 5 6 7 8 9 10

Then do it for the rest of the no.s ie 5 and 10 (Here 7 dont have multiple)

Finally print the non highlighted elements. 2,3,5,7.

Repeat this procedure for any other ranges.

Since you are interested in writing computer programs for prime numbers, perhaps you would like to turn this paper into a computer program. It deals with prime numbers and is similar to the sieve you were trying to create in C.

https://graviticpropulsion.files.wordpress.com/2015/04/prime-number-theory.pdf

I'm not sure if it is faster or less memory intensive, but I'm curious myself how high of a prime number it can find before it becomes too intensive for a computer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM