Question

I was trying to add some linked list to my recursive prime number codes, I was able to store the values using linked list then when I was to retrieve the prime numbers between the two inputted numbers i got this as a result.

for input 1 and 5: 1, 21, 301, 5

output should be:

2, 3, 5

the code is:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

struct node
{
    //struct node *prev;
    int num;
    struct node *nxt;
}*head;

void store(int value){
    struct node *var, *temp;
    var = (struct node *)malloc(sizeof(struct node));
    var->num = value;
    if(head==NULL){
        head = var;
        head->nxt = NULL;
    }else{
        temp = var;
        temp->nxt = head;
        head=temp;
    }
}

void accept(int value, int i){
    if(i<2){
        printf("Enter value: ");
        scanf("%d", &value);
        store(value);
        i++;
        accept(value,i);
    }
}

void prime(){
    int num,x,y;
    struct node *temp,*temp2,*var;
    temp = head;
    temp2 = temp->nxt;
    y = temp->num;
    x = temp2->num;
    primeloop(x,y);
}

int primeloop(int x,int y){
    int num;
    if ( x == 1 ) x++;
    if(x <= y){
        num = isPrime(x,2); // second input parameter added
        printf("%d",num);
        if(num == 0){
             printf("");
         }else{
             printf("%5d",x);
         }
        primeloop(x+1,y);
    }
}

int isPrime(int n, int i){
    if(n%i==0 && n!=2 && n!=i){
      return(0);
    } else {
       if (i < sqrt(n)) {
        return( isPrime(n,i+1) );
    } else
     return(1);
    }
}

void main(){
    int i,value;
    clrscr();
    i = 0;
    accept(value,i);
    prime();
    getch();
}

I had to change some lines to make it work with linked list, I could be missing something here since the algo is still the same. Do point out what I did wrong.

Was it helpful?

Solution

I found your problem I think. Your algorithm is correct, but you have two redundant printfs.

your primeloop should be like this

int primeloop(int x,int y){
    int num;
    if ( x == 1 ) x++;
    if(x <= y){
        num = isPrime(x,2); // second input parameter added
        if(num != 0){
            printf("%5d ",x);
        }
        primeloop(x+1,y);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top