Saturday, January 11, 2025
Google search engine
HomeData Modelling & AIHexagonal Number

Hexagonal Number

Given an integer n, the task is to find the nth hexagonal number . The nth hexagonal number Hn is the number of distinct dots in a pattern of dots consisting of the outlines of regular hexagons with sides up to n dots when the hexagons are overlaid so that they share one vertex.{Source : wiki}
 

Input: n = 2
Output: 6

Input: n = 5
Output: 45

Input: n = 7
Output: 91

 

In general, a polygonal number (triangular number, square number, etc) is a number represented as dots or pebbles arranged in the shape of a regular polygon. The first few pentagonal numbers are 1, 5, 12, etc. 
If s is the number of sides in a polygon, the formula for the nth s-gonal number P (s, n) is 
 

nth s-gonal number P(s, n) = (s - 2)n(n-1)/2 + n

If we put s = 6, we get

n'th Hexagonal number Hn = 2(n*n)-n 
                             = n(2n - 1) 

 

C++




// C++ program for above approach
#include<bits/stdc++.h>
using namespace std;
  
// Finding the nth Hexagonal Number
int hexagonalNum(int n){
    return n*(2*n - 1);
}
 
// Driver program to test above function
int main(){
    int n = 10;
    cout << "10th Hexagonal Number is "<< hexagonalNum(n) << endl;
    return 0;
}
 
// The code is contributed by Gautam goel (gautamgoel962)


C




// C program for above approach
#include <stdio.h>
#include <stdlib.h>
 
// Finding the nth Hexagonal Number
int hexagonalNum(int n)
{
    return n*(2*n - 1);
}
 
// Driver program to test above function
int main()
{
    int n = 10;
    printf("10th Hexagonal Number is = %d",
                             hexagonalNum(n));
 
    return 0;
}


Java




// Java program for above approach
class Hexagonal
{
    int hexagonalNum(int n)
    {
        return n*(2*n - 1);
    }
}
 
public class GeeksCode
{
    public static void main(String[] args)
    {
        Hexagonal obj = new Hexagonal();
        int n = 10;
        System.out.printf("10th Hexagonal number is = "
                          + obj.hexagonalNum(n));
    }
}


Python3




# Python program for finding Hexagonal numbers
def hexagonalNum( n ):
    return n*(2*n - 1)
 
# Driver code
n = 10
print ("10th Hexagonal Number is = ", hexagonalNum(n))


C#




// C# program for above approach
using System;
 
class GFG {
     
    static int hexagonalNum(int n)
    {
        return n * (2 * n - 1);
    }
 
    public static void Main()
    {
     
        int n = 10;
         
        Console.WriteLine("10th Hexagonal"
        + " number is = " + hexagonalNum(n));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program for above approach
 
// Finding the nth Hexagonal Number
function hexagonalNum($n)
{
    return $n * (2 * $n - 1);
}
 
// Driver program to test above function
$n = 10;
echo("10th Hexagonal Number is " .
                        hexagonalNum($n));
 
// This code is contributed by Ajit.
?>


Javascript




<script>
 
// Javascript program for above approach
 
// centered pentadecagonal function
function hexagonalNum(n)
{
    return n * (2 * n - 1);
}
 
// Driver Code
var n = 10;
document.write("10th Hexagonal number is = " +
               hexagonalNum(n));
 
// This code is contributed by Kirti
     
</script>


Output: 

10th Hexagonal Number is =  190

Time complexity: O(1) since performing constant operations

Auxiliary space: O(1) since it is using constant variables

Reference:https://en.wikipedia.org/wiki/Hexagonal_number
This article is contributed by Nishant_Singh(pintu). If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeek’s main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments