Given a number N. The task is to write a program to find the N-th term in the below series:
1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64, 729, 128, 2187…
Examples:
Input : 4
Output : 3
Input : 11
Output : 32
On observing carefully, you will find that the series is a mixture of 2 series:
- All the odd terms in this series form a geometric series.
- All the even terms form yet another geometric series.
The approach to solving the problem is quite simple. The odd positioned terms in the given series form a GP series with first term = 1 and common ration = 2. Similarly, the even positioned terms in the given series form a GP series with first term = 1 and common ration = 3.
Therefore first check whether the input number N is even or odd. If it is even, set N=N/2(since there are Two GP series running parallelly) and find the Nth term by using formula an = a1·rn-1 with r=3.
Similarly, if N is odd, set N=(n/2)+1 and do the same as previous with r=2.
Below is the implementation of above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
void findNthTerm( int n)
{
if (n % 2 == 0) {
n = n / 2;
cout << pow (3, n - 1) << endl;
}
else {
n = (n / 2) + 1;
cout << pow (2, n - 1) << endl;
}
}
int main()
{
int N = 4;
findNthTerm(N);
N = 11;
findNthTerm(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
static void findNthTerm( int n)
{
if (n % 2 == 0 )
{
n = n / 2 ;
System.out.print(Math.pow( 3 , n - 1 ) + "\n" );
}
else
{
n = (n / 2 ) + 1 ;
System.out.print(Math.pow( 2 , n - 1 ) + "\n" );
}
}
public static void main(String[] args)
{
int N = 4 ;
findNthTerm(N);
N = 11 ;
findNthTerm(N);
}
}
|
Python3
def findNthTerm(n):
if n % 2 = = 0 :
n / / = 2
print ( 3 * * (n - 1 ))
else :
n = (n / / 2 ) + 1
print ( 2 * * (n - 1 ))
if __name__ = = '__main__' :
N = 4
findNthTerm(N)
N = 11
findNthTerm(N)
|
C#
using System;
class GFG
{
static void findNthTerm( int n)
{
if (n % 2 == 0)
{
n = n / 2;
Console.WriteLine(Math.Pow(3, n - 1));
}
else
{
n = (n / 2) + 1;
Console.WriteLine(Math.Pow(2, n - 1));
}
}
public static void Main()
{
int N = 4;
findNthTerm(N);
N = 11;
findNthTerm(N);
}
}
|
PHP
<?php
function findNthTerm( $n )
{
if ( $n % 2 == 0)
{
$n = $n / 2;
echo pow(3, $n - 1) . "\n" ;
}
else
{
$n = ( $n / 2) + 1;
echo pow(2, intval ( $n - 1)) . "\n" ;
}
}
$N = 4;
findNthTerm( $N );
$N = 11;
findNthTerm( $N );
?>
|
Javascript
<script>
function findNthTerm(n)
{
if (n % 2 == 0) {
n = Math.floor(n / 2);
document.write(Math.pow(3, n - 1) + "<br>" );
}
else {
n = Math.floor(n / 2) + 1;
document.write(Math.pow(2, n - 1) + "<br>" );
}
}
let N = 4;
findNthTerm(N);
N = 11;
findNthTerm(N);
</script>
|
Time Complexity: O(log2n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Method#2: Using Memoization
Approach
this approach uses memoization technique to store the already calculated values in a dictionary. If the value for nth term is already in the dictionary, the function returns the value from the dictionary. If not, it recursively calculates the nth term using the formula based on whether n is odd or even, stores the result in the dictionary, and returns the result.
Algorithm
1. Define a function to find the nth term in the given series.
2. Create a memo dictionary to store the already calculated values.
3. If the value for nth term is already in memo, return the value from memo.
4. If n is 1 or 2, return 1.
5. If n is odd, recursively call the function with n-1 and divide the result by 2.
6. If n is even, recursively call the function with n/2 and multiply the result by 3.
7. Store the result in memo and return the result.
C++
#include <bits/stdc++.h>
using namespace std;
int find_nth_term( int n, unordered_map< int , int >& memo) {
if (memo.count(n)) {
return memo[n];
}
if (n == 1 || n == 2) {
return 1;
}
int result;
if (n % 2 == 1) {
result = find_nth_term(n - 1, memo) / 2;
} else {
result = find_nth_term(n / 2, memo) * 3;
}
memo[n] = result;
return result;
}
int main() {
int n = 4;
unordered_map< int , int > memo;
cout << "The nth term in the given series is " << find_nth_term(n, memo);
return 0;
}
|
Python3
def find_nth_term(n, memo):
if n in memo:
return memo[n]
if n = = 1 or n = = 2 :
return 1
elif n % 2 = = 1 :
result = find_nth_term(n - 1 , memo) / / 2
else :
result = find_nth_term(n / / 2 , memo) * 3
memo[n] = result
return result
n = 4
memo = {}
print ( "The nth term in the given series is" , find_nth_term(n, memo))
|
Javascript
function findNthTerm(n, memo) {
if (n in memo) {
return memo[n];
}
if (n == 1 || n == 2) {
return 1;
} else if (n % 2 == 1) {
result = findNthTerm(n - 1, memo) / 2;
} else {
result = findNthTerm(n / 2, memo) * 3;
}
memo[n] = result;
return result;
}
let n = 4;
let memo = {};
console.log( "The nth term in the given series is" , findNthTerm(n, memo));
|
Java
import java.util.HashMap;
public class Main {
public static int findNthTerm( int n, HashMap<Integer, Integer> memo) {
if (memo.containsKey(n)) {
return memo.get(n);
}
if (n == 1 || n == 2 ) {
return 1 ;
}
int result;
if (n % 2 == 1 ) {
result = findNthTerm(n - 1 , memo) / 2 ;
} else {
result = findNthTerm(n / 2 , memo) * 3 ;
}
memo.put(n, result);
return result;
}
public static void main(String[] args) {
int n = 4 ;
HashMap<Integer, Integer> memo = new HashMap<>();
System.out.println( "The nth term in the given series is " + findNthTerm(n, memo));
}
}
|
Output
The nth term in the given series is 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Apr, 2023
Like Article
Save Article