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.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
#define MAX 1000
using namespace std;
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 ;
}
}
}
int NthFib( int n)
{
int f[n + 2];
int i;
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++) {
f[i] = f[i - 1] + f[i - 2];
}
return f[n];
}
void findNthTerm( int n)
{
if (n % 2 == 0) {
n = n / 2;
n = NthPrime(n);
cout << n << endl;
}
else {
n = (n / 2) + 1;
n = NthFib(n - 1);
cout << n << endl;
}
}
int main()
{
int X = 5;
findNthTerm(X);
X = 10;
findNthTerm(X);
return 0;
}
|
Java
class GFG
{
static int MAX = 1000 ;
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 ;
}
static int NthFib( int n)
{
int []f = new int [n + 2 ];
int i;
f[ 0 ] = 0 ;
f[ 1 ] = 1 ;
for (i = 2 ; i <= n; i++)
{
f[i] = f[i - 1 ] + f[i - 2 ];
}
return f[n];
}
static void findNthTerm( int n)
{
if (n % 2 == 0 )
{
n = n / 2 ;
n = NthPrime(n);
System.out.println(n);
}
else
{
n = (n / 2 ) + 1 ;
n = NthFib(n - 1 );
System.out.println(n);
}
}
public static void main(String[] args)
{
int X = 5 ;
findNthTerm(X);
X = 10 ;
findNthTerm(X);
}
}
|
Python 3
from math import sqrt
MAX = 1000
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
def NthFib(n) :
f = [ 0 ] * (n + 2 )
f[ 0 ], f[ 1 ] = 0 , 1
for i in range ( 2 , n + 1 ) :
f[i] = f[i - 1 ] + f[i - 2 ]
return f[n]
def findNthTerm(n) :
if n % 2 = = 0 :
n / / = 2
n = NthPrime(n)
print (n)
else :
n = (n / / 2 ) + 1
n = NthFib(n - 1 )
print (n)
if __name__ = = "__main__" :
X = 5
findNthTerm(X)
X = 10
findNthTerm(X)
|
C#
using System;
class GFG
{
static int MAX = 1000;
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;
}
static int NthFib( int n)
{
int []f = new int [n + 2];
int i;
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
f[i] = f[i - 1] + f[i - 2];
}
return f[n];
}
static void findNthTerm( int n)
{
if (n % 2 == 0)
{
n = n / 2;
n = NthPrime(n);
Console.WriteLine(n);
}
else
{
n = (n / 2) + 1;
n = NthFib(n - 1);
Console.WriteLine(n);
}
}
public static void Main()
{
int X = 5;
findNthTerm(X);
X = 10;
findNthTerm(X);
}
}
|
PHP
<?php
$MAX = 1000;
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 NthFib( $n )
{
$f = array ( $n + 2);
$f [0] = 0;
$f [1] = 1;
for ( $i = 2; $i <= $n ; $i ++)
{
$f [ $i ] = $f [ $i - 1] +
$f [ $i - 2];
}
return $f [ $n ];
}
function findNthTerm( $n )
{
if ( $n % 2 == 0)
{
$n = $n / 2;
$n = NthPrime( $n );
echo $n . "\n" ;
}
else
{
$n = ( $n / 2) + 1;
$n = NthFib( $n - 1);
echo $n . "\n" ;
}
}
$X = 5;
findNthTerm( $X );
$X = 10;
findNthTerm( $X );
?>
|
Javascript
<script>
let MAX =1000;
function 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 NthFib( n)
{
var f= new Int16Array(n+2).fill(0);
let i;
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++) {
f[i] = f[i - 1] + f[i - 2];
}
return f[n];
}
function findNthTerm( n)
{
if (n % 2 == 0) {
n = n / 2;
n = NthPrime(n);
document.write(n + "<br/>" );
}
else {
n = parseInt(n / 2) + 1;
n = NthFib(n - 1);
document.write(n + "<br/>" );
}
}
let X = 5;
findNthTerm(X);
X = 10;
findNthTerm(X);
</script>
|
Time Complexity: O(MAX*sqrt(MAX)), where MAX represents a defined constant.
Auxiliary Space: O(X), where X represents the given integer.