Find n-th term in the series 7, 15, 32, …
Last Updated :
17 Feb, 2023
Given a series 7, 15, 32, …… Find the nth term of this series.
Examples :
Input : 5
Output : 138
Input : 7
Output : 568
Approach: By seeing the pattern of the series we can easily identify that it is a mixed series.
S = 7, 15, 32…..
Each element in the series is multiplied by 2 and then incremented one more than the previous.
S = 7, 15 (2 * 7 + 1), 32 (2 * 15 + 2)…….
By using iteration we can easily find nth term of the series.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int findTerm( int n)
{
if (n == 1)
return n;
else {
int term = 7;
for ( int i = 2; i <= n; i++)
term = term * 2 + (i - 1);
return term;
}
}
int main()
{
int n = 5;
cout << findTerm(n);
return 0;
}
|
Java
import java.lang.*;
class GFG {
static int findTerm( int n)
{
if (n == 1 )
return n;
else {
int term = 7 ;
for ( int i = 2 ; i <= n; i++)
term = term * 2 + (i - 1 );
return term;
}
}
public static void main(String[] args)
{
int n = 5 ;
System.out.print(findTerm(n));
}
}
|
Python3
def findTerm(n) :
if n = = 1 :
return n
else :
term = 7
for i in range ( 2 , n + 1 ) :
term = term * 2 + (i - 1 );
return term;
print (findTerm( 5 ))
|
C#
using System;
class GFG {
static int findTerm( int n)
{
if (n == 1)
return n;
else {
int term = 7;
for ( int i = 2; i <= n; i++)
term = term * 2 + (i - 1);
return term;
}
}
public static void Main()
{
int n = 5;
Console.WriteLine(findTerm(n));
}
}
|
PHP
<?php
function findTerm( $n )
{
if ( $n == 1)
return $n ;
else
{
$term = 7;
for ( $i = 2; $i <= $n ; $i ++)
$term = $term * 2 + ( $i - 1);
return $term ;
}
}
$n = 5;
echo (findTerm( $n ));
?>
|
Javascript
<script>
function findTerm(n)
{
if (n == 1)
return n;
else
{
let term = 7;
for (let i = 2; i <= n; i++)
term = term * 2 + (i - 1);
return term;
}
}
let n = 5;
document.write(findTerm(n));
</script>
|
Time Complexity: O(n) since for loop will run for n times in worst case
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...