The Second hexagonal numbers series can be represented as
3, 10, 21, 36, 55, 78, 105, 136, 171, 210, 253, …..
Nth term
Given an integer N. The task is to find the N-th term of the given series.
Examples:
Input: N = 1
Output: 3
Input: N = 4
Output: 36
Approach: The idea is to find the general term for the Second hexagonal numbers. Below is the computation of the general term for second hexagonal numbers:
1st Term = 1 * (2*1 + 1) = 3
2nd term = 2 * (2*2 + 1) = 10
3rd term = 3 * (2*3 + 1) = 21
4th term = 4 * (2*4 + 1) = 36
.
.
.
Nth term = n * (2*n + 1)
Therefore, the Nth term of the series is given as
Below is the implementation of above approach:
C++
// C++ implementation to // find N-th term // in the series #include <iostream> #include <math.h> using namespace std; // Function to find N-th term // in the series void findNthTerm( int n) { cout << n * (2 * n + 1) << endl; } // Driver code int main() { int N = 4; findNthTerm(N); return 0; } |
Java
// Java implementation to // find N-th term // in the series class GFG{ // Function to find N-th term // in the series static void findNthTerm( int n) { System.out.print(n * ( 2 * n + 1 )); } // Driver code public static void main (String[] args) { int N = 4 ; findNthTerm(N); } } // This code is contributed by Ritik Bansal |
Python3
# Python3 implementation to # find N-th term # in the series # Function to find N-th term # in the series def findNthTerm(n): print (n * ( 2 * n + 1 )) # Driver code N = 4 # Function call findNthTerm(N) # This code is contributed by Vishal Maurya |
C#
// C# implementation to // find N-th term // in the series using System; class GFG{ // Function to find N-th term // in the series static void findNthTerm( int n) { Console.Write(n * (2 * n + 1)); } // Driver code public static void Main() { int N = 4; findNthTerm(N); } } // This code is contributed by Code_Mech |
Javascript
<script> // Javascript implementation to // find N-th term // in the series // Function to find N-th term // in the series function findNthTerm(n) { document.write(n * (2 * n + 1)); } // Driver code N = 4; findNthTerm(N); </script> |
36
Time Complexity: O(1)
Auxiliary Space: O(1)
Reference:OEIS