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:
- 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++
// 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
filter_none
Java
// 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
filter_none
Python 3
# 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
filter_none
C#
// 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
filter_none
PHP
<?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
filter_none
Output:
1 11
Recommended Posts:
- Program to find N-th term of the series a, b, b, c, c, c,.......
- Program to find Nth term of series 0, 9, 22, 39, 60, 85, 114, 147, .....
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find Nth term of series 7, 21, 49, 91, 147, 217, ......
- Program to find N-th term of series 1, 2, 11, 12, 21….
- Program to find the Nth term of series 5, 12, 21, 32, 45......
- Program to find Nth term of the series 3 , 5 , 21 , 51 , 95 , ...
- Program to find Nth term of the series 3, 12, 29, 54, 87, ...
- Program to find N-th term of series 3, 5, 33, 35, 53....
- Program to find Nth term of the series 3, 6, 18, 24, ...
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
- Program to find Nth term of the series 2, 4, 3, 4, 15...
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
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.