Centered pentagonal number - GeeksforGeeks

09 Nov, 2021
Given an integer n, find the nth Centered pentagonal number. 
A Centered Pentagonal Number is a centered figurate number that represents a pentagon with a dot in the center and other dots surrounding it in pentagonal layers successively [ Source: Wiki ]
Few Centered pentagonal Number are : 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 331, 391....................
Examples : 
Input : 3
Output : 16

Input : 9
Output : 181
Approach: Centered pentagonal for n-th term is given by :  
Basic implementation of the above approach : 
C++
Java
Python3
C#
PHP
Javascript
// Program to find nth// Centered pentagonal number.#include <bits/stdc++.h>using namespace std; // centered pentagonal number function int centered_pentagonal_Num(int n){    // Formula to calculate nth    // Centered pentagonal number    // and return it into main function.     return (5 * n * n - 5 * n + 2) / 2;} // Driver Codeint main(){    int n = 7;    cout << n << "th Centered pentagonal number: ";    cout << centered_pentagonal_Num(n);    return 0;}
// Program to find nth// Centered pentagonal numberimport java.io.*; class GFG{     // centered pentagonal// number functionstatic int centered_pentagonal_Num(int n){    // Formula to calculate    // nth Centered pentagonal    // number and return it    // into main function.     return (5 * n * n - 5 * n + 2) / 2;} // Driver Codepublic static void main (String[] args){int n = 7;System.out.print(n + "th Centered " +              "pentagonal number: ");System.out.println(centered_pentagonal_Num(n));}} // This code is contributed by anuj_67.
# Python program to find Nth# Centered pentagonal number. # Function to calculate# Centered pentagonal number. def centered_pentagonal_Num(n):     # Formula to calculate nth    # Centered pentagonal number.         return (5 * n * n - 5 * n + 2) // 2 # Driver Coden = 7print("%sth Centered pentagonal number : " %n,                    centered_pentagonal_Num(n))                     # This code is contributed by ajit                
// C# Program to find nth// Centered pentagonal numberusing System; class GFG{     // centered pentagonal// number functionstatic int centered_pentagonal_Num(int n){    // Formula to calculate    // nth Centered pentagonal    // number and return it    // into main function.     return (5 * n * n - 5 * n + 2) / 2;} // Driver Codepublic static void Main (){int n = 7;Console.Write(n + "th Centered " +           "pentagonal number: ");Console.WriteLine(centered_pentagonal_Num(n));}} // This code is contributed by anuj_67.
<?php// PHP Program to find nth// Centered pentagonal number. // Centered pentagonal number function function centered_pentagonal_Num($n){    // Formula to calculate nth    // Centered pentagonal number    // and return it into main function.     return (5 * $n * $n - 5 * $n + 2) / 2;} // Driver Code$n = 7;echo $n , "th Centered pentagonal number: ";echo centered_pentagonal_Num($n); // This code is contributed by aj_36?>
<script>// Program to find nth// Centered pentagonal number      // centered pentagonal// number functionfunction centered_pentagonal_Num(n){     // Formula to calculate    // nth Centered pentagonal    // number and return it    // into main function.    return (5 * n * n - 5 * n + 2) / 2;} // Driver Codevar n = 7;document.write(n + "th Centered " +              "pentagonal number: ");document.write(centered_pentagonal_Num(n)); // This code is contributed by Amit Katiyar</script>
Output :  
7th Centered pentagonal number: 106
Time Complexity: O(1)Auxiliary Space: O(1) 
jit_t
vt_m
amit143katiyar
manikarora059
chhabradhanvi
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for distance between two points on earth
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Convex Hull | Set 2 (Graham Scan)
Given n line segments, find if any two segments intersect
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7