Bei einer gegebenen Zahl n besteht die Aufgabe darin, die N-te Siebeneckzahl zu finden. Eine siebeneckige Zahl stellt ein Siebeneck dar und gehört zu einer figurativen Zahl. Heptagonal hat sieben Winkel, sieben Eckpunkte und ein siebenseitiges Polygon. 
Beispiele:
 

Eingang: 2 
Ausgang: 7
Eingang: 15 
Ausgang: 540 
 

Siebeneckig

Einige Siebeneckzahlen sind: 
1, 7, 18, 34, 55, 81, 112, 148, 189, 235………..
Eine Formel zur Berechnung der N-ten Siebeneckzahl: 
 

\begin{math}  Hep_{n}=((5*n*n)-3*n)/2 \end{math}

C++

// C++ program to find the
// nth Heptagonal number
#include <iostream>
using namespace std;
 
// Function to return Nth Heptagonal
// number
int heptagonalNumber(int n)
{
    return ((5 * n * n) - (3 * n)) / 2;
}
 
// Drivers Code
int main()
{
 
    int n = 2;
    cout << heptagonalNumber(n) << endl;
    n = 15;
    cout << heptagonalNumber(n) << endl;
 
    return 0;
}

Java

// Java program to find the
// nth Heptagonal number
import java.io.*;
 
class GFG
{
// Function to return
// Nth Heptagonal number
static int heptagonalNumber(int n)
{
    return ((5 * n * n) - (3 * n)) / 2;
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 2;
    System.out.println(heptagonalNumber(n));
    n = 15;
    System.out.println(heptagonalNumber(n));
}
}
 
// This code is contributed by anuj_67.

Python3

# Program to find nth
# Heptagonal number
 
# Function to find
# nth Heptagonal number
def heptagonalNumber(n) :
     
    # Formula to calculate
    # nth Heptagonal number
    return ((5 * n * n) -
            (3 * n)) // 2
 
# Driver Code
if __name__ == '__main__' :
    n = 2
    print(heptagonalNumber(n))
    n = 15
    print(heptagonalNumber(n))
                 
# This code is contributed
# by ajit

C#

// C# program to find the
// nth Heptagonal number
using System;
 
class GFG
{
// Function to return
// Nth Heptagonal number
static int heptagonalNumber(int n)
{
    return ((5 * n * n) -
            (3 * n)) / 2;
}
 
// Driver Code
public static void Main ()
{
    int n = 2;
    Console.WriteLine(heptagonalNumber(n));
    n = 15;
    Console.WriteLine(heptagonalNumber(n));
}
}
 
// This code is contributed by anuj_67.

PHP

<?php
// PHP program to find the
// nth Heptagonal number
 
// Function to return Nth
// Heptagonal number
function heptagonalNumber($n)
{
    return ((5 * $n * $n) -
            (3 * $n)) / 2;
}
 
// Driver Code
$n = 2;
echo heptagonalNumber($n), "\n";
$n = 15;
echo heptagonalNumber($n);
 
// This code is contributed
// by anuj_67.
?>

Javascript

<script>
// Javascript program to find the
// nth Heptagonal number
 
// Function to return Nth Heptagonal
// number
function heptagonalNumber(n)
{
    return parseInt(((5 * n * n) - (3 * n)) / 2);
}
 
// Drivers Code
let n = 2;
document.write(heptagonalNumber(n) + "<br>");
n = 15;
document.write(heptagonalNumber(n) + "<br>");
 
// This code is contributed by rishavmahato348.
</script>
Ausgabe : 
7
540

 

Zeitkomplexität: O(1)
Hilfsraum: O(1)

Referenz: https://en.wikipedia.org/wiki/Heptagonal_number
 

Falls Sie an Live-Kursen mit Experten teilnehmen möchten , beziehen Sie sich bitte auf DSA Live-Kurse für Berufstätige und Competitive Programming Live for Students .