Fibonacci - Prime Series

☞ Consider the below series:
1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, …
This series is a mixture of 2 series – all the odd terms in this series form a Fibonacci series and all the even terms are the prime numbers in ascending order.
Write a program to find the Nth term in this series.
Program
#include<stdio.h>
int main(){
    int pos,i=1,j,a=0,b=1,c=0,p=1,ele;
    scanf("%d",&pos);
    a=b;
    while(1){
        a=b-a;
        if(p==pos){
            ele=b;
            break;
        }
        b=a+b;
        p++;
        while(1){
            c=0;
            for(j=1;j<=i/2;j++){
                if(i%j==0)
                    c++;
            }
            if(c==1){
                ele=i;
                i++;
                break;
            }
            else    i++;
        }
        if(p==pos){
            break;
        }
        p++;
    }
    printf("%d",ele);
}
Test Program

No comments:

Post a Comment

Total Pageviews