Find the Nth term of the series 9, 45, 243,1377
Given an integer N, the task is to print the Nth term of the series 9, 45, 243, 1377, 8019, …
Examples:
Input: N = 3
Output: 243
Input: N = 5
Output: 8019
Approach: Let the Nth term be An, we can get the Nth term easily by observing the series:
9, 45, 243, 1377, 8019, …
(11 + 21) * 31, (12 + 22) * 32, (13 + 23) * 33, (14 + 24) * 34, …, (1n + 2n) * 3n
So, An = (1n + 2n) * 3n
Algorithm:
Step 1: Start
step 2: create a function of static type with int return type with an integer parameter as input.
Step 3: Now we will use the pow function to raise the numbers 1, 2, and 3 to the power of n.
Step 4: Now it will return the value of the nth term.
Step 5: End
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
long nthterm( int n)
{
int An = ( pow (1, n) + pow (2, n)) * pow (3, n);
return An;
}
int main()
{
int n = 3;
cout << nthterm(n);
return 0;
}
|
Python
def nthterm(n):
An = ( 1 * * n + 2 * * n) * ( 3 * * n)
return An;
n = 3
print (nthterm(n))
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
public class GFG {
static int nthTerm( int n)
{
int An
= (( int )Math.pow( 1 , n) + ( int )Math.pow( 2 , n))
* ( int )Math.pow( 3 , n);
return An;
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println(nthTerm(n));
}
}
|
C#
using System;
public class GFG {
static int nthTerm( int n)
{
int An
= (( int )Math.Pow(1, n) + ( int )Math.Pow(2, n))
* ( int )Math.Pow(3, n);
return An;
}
public static void Main()
{
int n = 3;
Console.WriteLine(nthTerm(n));
}
}
|
PHP
<?php
function nthterm( $n )
{
$An = (pow(1, $n ) + pow(2, $n )) * pow(3, $n );
return $An ;
}
$n = 3;
echo nthterm( $n );
?>
|
Javascript
<script>
function nthterm(n)
{
let An = (Math.pow(1, n) + Math.pow(2, n)) * Math.pow(3, n);
return An;
}
let n = 3;
document.write(nthterm(n));
</script>
|
Time Complexity: O(log n)
Auxiliary Space: O(1)
Last Updated :
26 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...