Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…

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:

  1. Terms at odd positions in the given series forms fibonacci series.
  2. 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.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find N-th term
// in the series
#include<bits/stdc++.h>
#define MAX 1000
using namespace std;
  
// Function to find Nth Prime Number
int 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 Number
int 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 series
void 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 code
int main()
{
    int X = 5;
    findNthTerm(X);
  
    X = 10;
    findNthTerm(X);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find N-th 
// term in the series
class GFG 
{
  
static int MAX = 1000;
  
// Function to find Nth Prime Number
static 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 Number
static 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 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 series
static void findNthTerm(int n)
{
// If n is even
if (n % 2 == 0
{
    n = n / 2;
    n = NthPrime(n);
    System.out.println(n);
}
  
// If n is odd
else 
{
    n = (n / 2) + 1;
    n = NthFib(n - 1);
    System.out.println(n);
}
}
  
// Driver code
public static void main(String[] args) 
{
    int X = 5;
    findNthTerm(X);
  
    X = 10;
    findNthTerm(X);
}
}
  
// This code is contributed 
// by ChitraNayal

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find N-th 
# term in the series 
  
# import sqrt method from math module
from math import sqrt
  
# Globally declare constant value
MAX = 1000
  
# Function to find Nth Prime Number
def 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 Number
def 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 series 
def 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 code
if __name__ == "__main__" :
  
    X = 5
  
    # function calling
    findNthTerm(X)
  
    X = 10
    findNthTerm(X)
      
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find N-th term
// in the series
using System;
  
class GFG 
{
static int MAX = 1000;
  
// Function to find Nth Prime Number
static 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 Number
static 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 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 series
static void findNthTerm(int n)
{
// If n is even
if (n % 2 == 0) 
{
    n = n / 2;
    n = NthPrime(n);
    Console.WriteLine(n);
}
  
// If n is odd
else 
{
    n = (n / 2) + 1;
    n = NthFib(n - 1);
    Console.WriteLine(n);
}
}
  
// Driver code
public static void Main()
{
    int X = 5;
    findNthTerm(X);
  
    X = 10;
    findNthTerm(X);
}
}
  
// This code is contributed 
// by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// N-th term in the series
$MAX = 1000;
  
// Function to find
// Nth Prime Number
function 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 Number
function 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 series
function 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
?>

chevron_right


Output:

1
11


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.