Series of Fibonacci and Prime numbers | 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.


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. 

The value N is a Positive integer that should be read from STDIN. The Nth term that is calculated by the program should be written to STDOUT. Other than the value of Nth term, no other characters/strings or message should be written to STDOUT. 


For example, when N = 14, the 14th term in the series is 17. So only the value 17 should be printed to STDOUT.




#include<stdio.h>
#define MAX 1000
void fibonacci(int n)
{
int i, term1 = 0, term2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = term1 + term2;
term1 = term2;
term2 = nextTerm;
}
printf("%d", term1);
}

void prime(int n)
{
int i, j, flag, count =0;
for (i=2; i<=MAX; i++)
{
flag = 0;
for (j=2; j<i; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
count++;
if(count == n)
{
printf("%d", i);
break;
}
}
}
int main( )
{
int n;
scanf("%d", &n);
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}

Post a Comment

6 Comments

  1. Replies
    1. https://ideone.com/ldihuz
      I have executed it here....Check it out !!

      Delete
  2. Sorry , it's correct ,
    My compiler have some issue ...

    ReplyDelete
  3. /* package codechef; // don't place package name! */

    import java.util.*;
    import java.lang.*;
    import java.io.*;

    /* Name of the class has to be "Main" only if the class is public. */
    class Codechef
    {
    public static void main (String[] args) throws java.lang.Exception
    {
    // your code goes here
    Scanner sc=new Scanner(System.in);
    int n;
    n=sc.nextInt();
    int n1,n2;
    if(n%2==0){
    n1=evens(n/2);
    System.out.println(n1);
    }else{
    n2=odds(n/2+1);
    System.out.println(n2);
    }
    }
    static int odds(int n){
    if(n<=1){
    return n;
    }
    return odds(n-1) + odds(n-2);
    }
    static int evens(int n){
    int count=0,i;
    int num=1;
    while(count<n){
    num=num+1;
    for( i=2;i<=num;i++){
    if(num%i==0){
    break;
    }
    }
    if(i==num){
    count+=1;
    }
    }
    return num;

    }
    }

    ReplyDelete
  4. #include //1 2 1 3 2 5 3 7
    int main()
    {
    int n,i,j,count=0,k=0;
    scanf("%d",&n);
    int a[50];
    for(i=3;i<100;i++)
    {
    for(j=1;j<=i;j++)
    {
    if(i%j==0)
    {
    count++;
    }
    }
    if(count==2)
    {
    a[k]=i;
    k++;
    }
    count=0;
    }
    k=0;
    int b[n];//1 2 1 3 2 3
    b[0]=1;
    b[1]=2;
    b[2]=1;
    for(i=3;i<n;i++)
    {
    if(i%2==0)
    {
    b[i]=b[i-2]+b[i-4];
    }
    else
    {
    b[i]=a[k];
    k++;
    }
    }
    for(i=0;i<n;i++)
    {
    printf("%d ",b[i]);
    }
    printf("\n");
    printf("%d ",b[n-1]);
    return 0;
    }

    ReplyDelete