Prime number program in C

Gordon Ramsay

This program is supposed to print the first x prime numbers, but I noticed that it was printing some non-prime numbers, such as 27 or 35. I've been looking at it for hours and nothing seems to pop up. So please, if you know what's wrong, tell me.

#include <stdio.h>
int main(){
    int i=0, cont=2, prim=2, quant;
    printf("Insert number of prime numbers you wish: ");
    scanf("%d", &quant);
    printf("The first %d prime numbers are:\n", quant);
    while(i<quant){
        if(prim%cont!=0 && (cont>1 && cont<prim)){
            cont++;
        }
        else if(prim%cont==0 && (cont>1 && cont<prim)){
            prim++;
        }
        else if(prim%cont==0 && cont==prim){
            printf("%d\n", prim);
            prim++;
            cont=2;
            i++;
        }
    }
    return 0;
}

UPDATE

Wow, ok, so after 7 years people still stumble upon this question. In the last 7 years my coding ability has improved somewhat, and now I see how inefficient this program was. Just in case anyone might be trying to find the solution for this and the stumble upon this, here are three solutions much easier and better, and why they work.

Solution 1: This first solution is very inefficient too, but it works, and is pretty simple to understand. Basically what you have to do is check if each number up to the upper limit is prime or not. To do so, just check if it is divisible by any number up to its square root.

Why its squared root? Because the square root squared is equal to the number, which means that if the number was not divisible up to its square root, it won't be divisible by any number above it, as for it to be divisible by it, it needs to multiply by a smaller number. For example, the squared root of 36 is 6, and 36 is divisible by 9, as 9*4=36. But since 9 is above 6 (which is the squared root), the number that multiplied by 9 gives us 36 is below, and as such, we have already seen that it is not prime, as we have already checked that 36 is divisible by 4. This being said, if no number below the squared root of a number is a natural dividend of the number, than that number is prime.

#include <stdio.h>

int isPrime(int num) {
    for (int i = 2; i*i <= num; i++) {
        if (num%i==0) {
            return 0;
        }
    }
    return 1;
}

void getPrimes(int num) {
    int cont = 0;
    for (int i = 2; cont < num; i++) {
        if (isPrime(i)==1) {
            printf("%d\n", i);
            cont++;
        }
    }
}

int main() {
    int quant;
    printf("Insert number of prime numbers you wish: ");
    scanf("%d", &quant);
    printf("The first %d prime numbers are:\n", quant);
    getPrimes(quant);
    return 0;
}

This is inefficient as we are checking if the number is divisible by numbers that won't influence (more on this in the next solution).

Solution 2: In this second solution, which is more elegant in my opinion, we give use to the Fundamental Theorem of Arithmetic, which states that any number greater than 1 is either a prime number itself, or can be represented by factorizing into the multiplication of prime numbers. This means that if a number is divisible by a non prime number, it is also divisible by the prime numbers that make it up, and thus we only need to check if the number in question is divisible by smaller prime numbers than itself. For this purpose, we store the prime numbers in an array.

#include <stdio.h>

void getPrimes(int num) {
    int primes[num];
    int cont = 1;
    primes[0] = 2;
    int current = primes[cont-1]+1;
    while (cont < num) {
        int before = current;
        for (int i = 0; i < cont; i++) {
            if (current%primes[i]==0) {
                current++;
                break;
            }
        }
        if (before == current) {
            primes[cont] = current;
            cont++;
            current++;
        }
    }
    for (int i = 0; i < cont; i++) {
        printf("%d ", primes[i]);
    }
    printf("\n");
}

int main() {
    int quant;
    printf("Insert number of prime numbers you wish: ");
    scanf("%d", &quant);
    printf("The first %d prime numbers are:\n", quant);
    getPrimes(quant);
    return 0;
}

Solution 3: This final solution is a mix of the two above. Solution 1 checked numbers that weren't prime, having redundancy, and the solution 2 checked numbers above the square root, which can never be a dividend if the smaller numbers aren't. In this solution, we check if the number is divisible only by prime numbers below the squared root.

#include <stdio.h>

void getPrimes(int num) {
    int primes[num];
    int cont = 1;
    primes[0] = 2;
    int current = primes[cont-1]+1;
    while (cont < num) {
        int before = current;
        for (int i = 0; (i < cont && primes[i]*primes[i] <= current); i++) {
            if (current%primes[i]==0) {
                current++;
                break;
            }
        }
        if (before == current) {
            primes[cont] = current;
            cont++;
            current++;
        }
    }
    for (int i = 0; i < cont; i++) {
        printf("%d ", primes[i]);
    }
    printf("\n");
}

int main() {
    int quant;
    printf("Insert number of prime numbers you wish: ");
    scanf("%d", &quant);
    printf("The first %d prime numbers are:\n", quant);
    getPrimes(quant);
    return 0;
}

Comparing the execution times of each algorithm for 100 prime numbers, we obtain the following results

Algorithm 1 Algorithm 2 Algorithm 3
0.048 ms 0.068 ms 0.039 ms

Comparing to the algorithm of the original post which takes 0.599 ms, every one of these new solutions is more efficient (the worst being around 10x better), and actually calculates the real values.

iku

Try this

#include<stdio.h>

int prime(int n)
{
    int i, j, len=1, brk=0;
    int list[200]={2};
    for(i=2; i<=n; i++)
    {
        for(j=0; j<len; j++)
        {
            if(i%list[j]==0){
                brk=1;
                break;
            }
            else
            {
                brk=0;
            }
        }
        if(brk==0)
        {
            list[len]=i;
            len++;
        }
    }
    for(i=0; i<len; i++)
        printf("%d ",list[i]);
}

main()
{
    int i, n;
    scanf("%d",&n);
    prime(n);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C Program to find prime number

From Dev

Prime number program in haskell

From Dev

Prime Number program

From Dev

Ruby Prime number program

From Dev

Prolog Program To Check If A Number Is Prime

From Dev

Prolog Program To Check If A Number Is Prime

From Dev

Java: Prime Number Program Not Working

From Dev

Prime number program does not work

From Dev

C++ : using prime number and not a prime number

From Dev

Prime Number Generator in C

From Dev

Prime Number Generator in C

From Dev

TSQL program to find the largest prime number

From Dev

C++: Program to find the largest prime factor of a number, what is wrong in my code?

From Dev

C# - Need help creating program that simulates Prime Number behavior by drawing lines

From Dev

Function used to define whether a number is prime or not causes c++ program to crash

From Dev

Find prime number in C#

From Dev

Check if a number is prime in C#

From Dev

Prime number checker returns any number as a prime. C

From Dev

C program with nested loop to get prime numbers

From Dev

C program to print next five prime numbers

From Dev

Prime Factors in C++(Error in program)

From Dev

C++ Program to Display Numbers Between Two Intervals Check Whether a Number can be Express as Sum of Two Prime Numbers

From Dev

Problems with program which find prime number from 1 to 100

From Dev

C: How to get a 4096 bit prime number?

From Dev

C-greatest prime factor of a given number

From Dev

C finding out prime factor of large number

From Dev

Checking for prime number - C# logic

From Dev

C finding out prime factor of large number

From Dev

C++ Breaking even number into prime factor

Related Related

HotTag

Archive