Find n-th term in the series 9, 33, 73,129 …
Given a series 9, 33, 73, 129… Find the n-th term of the series.
Examples:
Input : n = 4
Output : 129
Input : n = 5
Output : 201
The given series has a pattern which is visible after subtracting it from itself after one shift
S = 9 + 33 + 73 + 129 + … tn-1 + tn
S = 9 + 33 + 73 + … tn-2 + tn-1 + tn
———————————————
0 = 9 + (24 + 40 + 56 + ….) - tn
Since 24 + 40 + 56.. series in A.P with
common difference of 16, we get
tn = 9 + [((n-1)/2)*(2*24 + (n-1-1)d)]
On solving this we get
tn = 8n2 +1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int series( int n)
{
return (8 * n * n) + 1;
}
int main()
{
int n = 5;
cout << series(n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int series( int n)
{
return ( 8 * n * n) + 1 ;
}
public static void main(String args[])
{
int n = 5 ;
System.out.println(series(n));
}
}
|
Python3
def series(n):
print (( 8 * n * * 2 ) + 1 )
series( 5 )
|
C#
using System;
class GFG {
static int series( int n)
{
return (8 * n * n) + 1;
}
public static void Main()
{
int n = 5;
Console.WriteLine(series(n));
}
}
|
PHP
<?php
function series( $n )
{
return (8 * $n * $n ) + 1;
}
$n = 5;
echo (series( $n ));
?>
|
Javascript
<script>
function series(n)
{
return (8 * n * n) + 1;
}
let n = 5;
document.write(series(n));
</script>
|
Time Complexity: O(1), as we are using not using any loop or recursion to traverse.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...