Find the nth term of the given series
Last Updated :
07 Jun, 2022
Given the first two terms of the series as 1 and 6 and all the elements of the series are 2 less than the mean of the number preceding and succeeding it. The task is to print the nth term of the series.
First few terms of the series are:
1, 6, 15, 28, 45, 66, 91, …
Examples:
Input: N = 3
Output: 15
Input: N = 1
Output: 1
Approach: The given series represents odd positioned numbers in the triangular number series. Since the nth triangular number can easily be found by (n * (n + 1) / 2), so for finding the odd numbers we can replace n by (2 * n) – 1 as (2 * n) – 1 will always result in odd numbers i.e. the nth number of the given series will be ((2 * n) – 1) * n.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int oddTriangularNumber( int N)
{
return (N * ((2 * N) - 1));
}
int main()
{
int N = 3;
cout << oddTriangularNumber(N);
return 0;
}
|
Java
class GFG
{
static int oddTriangularNumber( int N)
{
return (N * (( 2 * N) - 1 ));
}
public static void main(String[] args)
{
int N = 3 ;
System.out.println(oddTriangularNumber(N));
}
}
|
Python3
def oddTriangularNumber(N):
return (N * (( 2 * N) - 1 ))
if __name__ = = '__main__' :
N = 3
print (oddTriangularNumber(N))
|
C#
using System;
class GFG
{
static int oddTriangularNumber( int N)
{
return (N * ((2 * N) - 1));
}
public static void Main(String[] args)
{
int N = 3;
Console.WriteLine(oddTriangularNumber(N));
}
}
|
PHP
<?php
function oddTriangularNumber( $N )
{
return ( $N * ((2 * $N ) - 1));
}
$N = 3;
echo oddTriangularNumber( $N );
?>
|
Javascript
<script>
function oddTriangularNumber(N)
{
return (N * ((2 * N) - 1));
}
let N = 3;
document.write(oddTriangularNumber(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...