Centered pentagonal number
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++
#include <bits/stdc++.h>
using namespace std;
int centered_pentagonal_Num( int n)
{
return (5 * n * n - 5 * n + 2) / 2;
}
int main()
{
int n = 7;
cout << n << "th Centered pentagonal number: " ;
cout << centered_pentagonal_Num(n);
return 0;
}
|
C
#include <stdio.h>
int centered_pentagonal_Num( int n)
{
return (5 * n * n - 5 * n + 2) / 2;
}
int main()
{
int n = 7;
printf ( "%dth Centered pentagonal number: " ,n);
printf ( "%d" ,centered_pentagonal_Num(n));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int centered_pentagonal_Num( int n)
{
return ( 5 * n * n - 5 * n + 2 ) / 2 ;
}
public static void main (String[] args)
{
int n = 7 ;
System.out.print(n + "th Centered " +
"pentagonal number: " );
System.out.println(centered_pentagonal_Num(n));
}
}
|
Python3
def centered_pentagonal_Num(n):
return ( 5 * n * n - 5 * n + 2 ) / / 2
n = 7
print ( "%sth Centered pentagonal number : " % n,
centered_pentagonal_Num(n))
|
C#
using System;
class GFG
{
static int centered_pentagonal_Num( int n)
{
return (5 * n * n - 5 * n + 2) / 2;
}
public static void Main ()
{
int n = 7;
Console.Write(n + "th Centered " +
"pentagonal number: " );
Console.WriteLine(centered_pentagonal_Num(n));
}
}
|
PHP
<?php
function centered_pentagonal_Num( $n )
{
return (5 * $n * $n - 5 * $n + 2) / 2;
}
$n = 7;
echo $n , "th Centered pentagonal number: " ;
echo centered_pentagonal_Num( $n );
?>
|
Javascript
<script>
function centered_pentagonal_Num(n)
{
return (5 * n * n - 5 * n + 2) / 2;
}
var n = 7;
document.write(n + "th Centered " +
"pentagonal number: " );
document.write(centered_pentagonal_Num(n));
</script>
|
Output :
7th Centered pentagonal number: 106
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
19 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...