Devarshi
Devarshi

Reputation: 11

C program to find the n'th prime number-

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int prime(long long int);
long long int *arr;         //array to hold n prime numbers
int main()
{
    int i,count=4;;
    long long int n;
    scanf("%lli",&n);
    arr=malloc(sizeof(long long int)*n);
    arr[0]=2;
    arr[1]=3;
    arr[2]=5;
    arr[3]=7;
    if (n==1) printf("%lli",arr[0]);
    else{ if (n==2) printf("%lli",arr[1]);
    else{ if (n==3) printf("%lli",arr[2]);
    else{ if (n==4) printf("%lli",arr[3]);
    else
    {
        for(i=2;count<n;i++)
        {
            if(prime(6*i-1)) {             /*As prime nos are always 6k+1 or 
            arr[count]=6*i-1;               6k-1fork>=2 I checked only for those*/
            count++; }
            if(prime(6*i+1)&&count<=n) {
            arr[count]=6*i+1;
            count++; }
            }
    printf("%lli",arr[count]);
    }}}}
    //free(arr);
return 0;
}

int prime(long long int x)
{
    int j=1,flag=1;
    while(arr[j]<=sqrt(x))
    {
        if (x%arr[j]==0)
        {
            flag=0;
            break;
        }
        j++;
    }
    return flag;
}
  1. The code is working only for n=1,2,3,4, i.e i=0,1,2,3 for which the values are explicitly given. For n=5 onwards it is giving 0 as O/P
  2. There is some glitch related to the global dynamic array as free(arr) is giving core dump error. Q: Is this the right way to declare a global dynamic array? What could be the problem in this code? Thank You in advance.

Upvotes: 0

Views: 2502

Answers (2)

CashCow
CashCow

Reputation: 31435

If that is your actual code you have 4 bugs:

  • 2 line comment scopes out a line of your code
  • the second if should check count < n not count <= n as if count == n you cannot write to arr[count]
  • You cannot print arr[count] only arr[count-1] which is probably what you mean
  • In the case where n is less than 4 you still set arr[1], arr[2] and arr[3] which may be out of bounds

It is of course also inefficient to call sqrt(x) in every loop iteration, potentially you should call it outside and there may be a potential rounding issue bug due to the way square roots are calculated, so you might prefer:

while( arr[j] * arr[j] < x )

It would be preferable not to make this global and to pass it into your function.

It would also be preferable to move the main loop logic of your program outside of main().

I'm surprised you say you program works for n=1, 2 and 3 as it looks like you are setting out of bounds.

Upvotes: 3

Yigal Reiss
Yigal Reiss

Reputation: 71

Your counter goes beyond the size of the array. Specifically both conditions (6i-1 and 6i+1) are met for i=2, and therefore counter is incremented twice, resulting in using arr[5] where you only allocated 5 places in the array. This is because you check counter<=n and not counter

Not sure this could be also be the reason for free creating a core dump, but it is possible (because once corrupting the memory, free may access corrupted data).

Upvotes: 2

Related Questions