Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,... - GeeksforGeeks

15 Mar, 2021
Given a number N. The task is to write a program to find the N-th term in the below series:  
0, 2, 1, 3, 1, 5, 2, 7, 3, ...  
Examples:  
Input: N = 5
Output: 1

Input: N = 10
Output: 11
 
When we look carefully at the series, we find that the series is a mixture of 2 series:  
Terms at odd positions in the given series forms fibonacci series.Terms at even positions in the given series forms a series of prime numbers. 
Terms at odd positions in the given series forms fibonacci series.
Terms at even positions in the given series forms a series of prime numbers. 
Now, To solve the above-given problem, first check whether the input number N is even or odd.  
If it is odd, set N=(N/2) + 1(since there are Two series running parallelly) and find the Nth fibonacci number.
If N is even, simply set N=N/2 and find Nth prime number.
Below is the implementation of above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// CPP program to find N-th term// in the series#include<bits/stdc++.h>#define MAX 1000using namespace std; // Function to find Nth Prime Numberint NthPrime(int n){    int count = 0;    for (int i = 2; i <= MAX; i++) {        int check = 0;        for (int j = 2; j <= sqrt(i); j++) {            if (i % j == 0) {                check = 1;                break;            }        }        if (check == 0)            count++;         if (count == n) {            return i;            break;        }    }} // Function to find Nth Fibonacci Numberint NthFib(int n){    // Declare an array to store    // Fibonacci numbers.    int f[n + 2];    int i;     // 0th and 1st number of the    // series are 0 and 1    f[0] = 0;    f[1] = 1;     for (i = 2; i <= n; i++) {        f[i] = f[i - 1] + f[i - 2];    }     return f[n];} // Function to find N-th term// in the seriesvoid findNthTerm(int n){    // If n is even    if (n % 2 == 0) {        n = n / 2;        n = NthPrime(n);        cout << n << endl;    }     // If n is odd    else {        n = (n / 2) + 1;        n = NthFib(n - 1);        cout << n << endl;    }} // Driver codeint main(){    int X = 5;    findNthTerm(X);     X = 10;    findNthTerm(X);     return 0;}
// Java program to find N-th// term in the seriesclass GFG{ static int MAX = 1000; // Function to find Nth Prime Numberstatic int NthPrime(int n){int count = 0;int i;for (i = 2; i <= MAX; i++){    int check = 0;    for (int j = 2; j <= Math.sqrt(i); j++)    {        if (i % j == 0)        {            check = 1;            break;        }    }    if (check == 0)        count++;     if (count == n)    {        return i;             }}    return 0;} // Function to find Nth Fibonacci Numberstatic int NthFib(int n){// Declare an array to store// Fibonacci numbers.int []f = new int[n + 2];int i; // 0th and 1st number of the// series are 0 and 1f[0] = 0;f[1] = 1; for (i = 2; i <= n; i++){    f[i] = f[i - 1] + f[i - 2];} return f[n];} // Function to find N-th term// in the seriesstatic void findNthTerm(int n){// If n is evenif (n % 2 == 0){    n = n / 2;    n = NthPrime(n);    System.out.println(n);} // If n is oddelse{    n = (n / 2) + 1;    n = NthFib(n - 1);    System.out.println(n);}} // Driver codepublic static void main(String[] args){    int X = 5;    findNthTerm(X);     X = 10;    findNthTerm(X);}} // This code is contributed// by ChitraNayal
# Python 3 program to find N-th# term in the series # import sqrt method from math modulefrom math import sqrt # Globally declare constant valueMAX = 1000 # Function to find Nth Prime Numberdef NthPrime(n) :         count = 0    for i in range(2, MAX + 1) :                 check = 0        for j in range(2, int(sqrt(i)) + 1) :                         if i % j == 0 :                check = 1                break         if check == 0 :            count += 1         if count == n :            return i            break # Function to find Nth Fibonacci Numberdef NthFib(n) :     # Create a list of size n+2    # to store Fibonacci numbers.    f = [0] * (n + 2)     # 0th and 1st number of the    # series are 0 and 1    f[0], f[1] = 0, 1     for i in range(2, n + 1) :         f[i] = f[i - 1] + f[i - 2]     return f[n] # Function to find N-th# term in the seriesdef findNthTerm(n) :     # If n is even    if n % 2 == 0 :        n //= 2        n = NthPrime(n)        print(n)     # If n is odd    else :        n = (n // 2) + 1        n = NthFib(n - 1)        print(n) # Driver codeif __name__ == "__main__" :     X = 5     # function calling    findNthTerm(X)     X = 10    findNthTerm(X)     # This code is contributed by ANKITRAI1
// C# program to find N-th term// in the seriesusing System; class GFG{static int MAX = 1000; // Function to find Nth Prime Numberstatic int NthPrime(int n){int count = 0;int i;for ( i = 2; i <= MAX; i++){    int check = 0;    for (int j = 2; j <= Math.Sqrt(i); j++)    {        if (i % j == 0)        {            check = 1;            break;        }    }    if (check == 0)        count++;     if (count == n)    {        return i;    }}    return 0;} // Function to find Nth Fibonacci Numberstatic int NthFib(int n){     // Declare an array to store// Fibonacci numbers.int []f = new int[n + 2];int i; // 0th and 1st number of the// series are 0 and 1f[0] = 0;f[1] = 1; for (i = 2; i <= n; i++){    f[i] = f[i - 1] + f[i - 2];} return f[n];} // Function to find N-th term// in the seriesstatic void findNthTerm(int n){// If n is evenif (n % 2 == 0){    n = n / 2;    n = NthPrime(n);    Console.WriteLine(n);} // If n is oddelse{    n = (n / 2) + 1;    n = NthFib(n - 1);    Console.WriteLine(n);}} // Driver codepublic static void Main(){    int X = 5;    findNthTerm(X);     X = 10;    findNthTerm(X);}} // This code is contributed// by ChitraNayal
<?php// PHP program to find// N-th term in the series$MAX = 1000; // Function to find// Nth Prime Numberfunction NthPrime($n){    global $MAX;    $count = 0;    for ($i = 2; $i <= $MAX; $i++)    {        $check = 0;        for ($j = 2;             $j <= sqrt($i); $j++)        {            if ($i % $j == 0)            {                $check = 1;                break;            }        }        if ($check == 0)            $count++;         if ($count == $n)        {            return $i;            break;        }    }} // Function to find// Nth Fibonacci Numberfunction NthFib($n){    // Declare an array to store    // Fibonacci numbers.    $f = array($n + 2);     // 0th and 1st number of    // the series are 0 and 1    $f[0] = 0;    $f[1] = 1;     for ($i = 2; $i <= $n; $i++)    {        $f[$i] = $f[$i - 1] +                 $f[$i - 2];    }     return $f[$n];} // Function to find N-th// term in the seriesfunction findNthTerm($n){    // If n is even    if ($n % 2 == 0)    {        $n = $n / 2;        $n = NthPrime($n);        echo $n . "\n";    }     // If n is odd    else    {        $n = ($n / 2) + 1;        $n = NthFib($n - 1);        echo $n . "\n";    }} // Driver code$X = 5;findNthTerm($X); $X = 10;findNthTerm($X); // This Code is contributed// by mits?>
<script> // JavaScript program to find N-th term// in the series let  MAX =1000;// Function to find Nth Prime Numberfunction NthPrime( n){    let count = 0;    for (let i = 2; i <= MAX; i++) {        let check = 0;        for (let j = 2; j <= Math.sqrt(i); j++) {            if (i % j == 0) {                check = 1;                break;            }        }        if (check == 0)            count++;         if (count == n) {            return i;            break;        }    }} // Function to find Nth Fibonacci Numberfunction NthFib( n){    // Declare an array to store    // Fibonacci numbers.    var f=new Int16Array(n+2).fill(0);         let i;     // 0th and 1st number of the    // series are 0 and 1    f[0] = 0;    f[1] = 1;     for (i = 2; i <= n; i++) {        f[i] = f[i - 1] + f[i - 2];    }      return f[n];} // Function to find N-th term// in the seriesfunction findNthTerm( n){    // If n is even    if (n % 2 == 0) {        n = n / 2;        n = NthPrime(n);        document.write(n +"<br/>");    }     // If n is odd    else {        n = parseInt(n / 2) + 1;        n = NthFib(n - 1);        document.write(n +"<br/>");    }} // Driver code     let X = 5;    findNthTerm(X);     X = 10;    findNthTerm(X); // This code contributed by aashish1995 </script>
1
11
 
Mithun Kumar
ankthon
ukasp
aashish1995
Fibonacci
Prime Number
series
Mathematical
School Programming
Mathematical
Prime Number
series
Fibonacci
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo Operator (%) in C/C++ with Examples
Print all possible combinations of r elements in a given array of size n
The Knight's tour problem | Backtracking-1
Operators in C / C++
Program for factorial of a number
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects