Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Program to check if N is a Centered Octagonal Number
Next article icon

Program to check if N is a Centered Pentagonal Number or not

Last Updated : 28 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number N, the task is to check if N is a Centered Pentagonal Number or not. If the number N is a Centered Pentagonal Number then print "Yes" else print "No".

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. The first few Centered Pentagonal Number are 1, 6, 16, 31, 51, 76, 106 ... 

Examples:  

Input: N = 6 
Output: Yes 
Explanation: 
Second Centered pentagonal number is 6.


Input: N = 20 
Output: No 

Approach: 
 

1. The Kth term of the Centered Pentagonal Number is given as
K^{th} Term = \frac{5*K^{2} - 5*K + 2}{2}        
 

2. As we have to check that the given number can be expressed as a Centered Pentagonal Number or not. This can be checked as:

=> N = \frac{5*K^{2} - 5*K + 2}{2}        
=> K = \frac{5 + \sqrt{40*N - 15}}{10}  

3. If the value of K calculated using the above formula is an integer, then N is a Centered Pentagonal Number.

4. Else the number N is not a Centered Pentagonal Number.

Below is the implementation of the above approach: 

C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;  // Function to check if number N // is a Centered pentagonal number bool isCenteredpentagonal(int N) {     float n         = (5 + sqrt(40 * N - 15))           / 10;      // Condition to check if N is a     // Centered pentagonal number     return (n - (int)n) == 0; }  // Driver Code int main() {     // Given Number     int N = 6;      // Function call     if (isCenteredpentagonal(N)) {         cout << "Yes";     }     else {         cout << "No";     }     return 0; } 
Java
// Java program for the above approach import java.util.*;  class GFG{  // Function to check if number N // is a centered pentagonal number static boolean isCenteredpentagonal(int N) {     float n = (float) ((5 + Math.sqrt(40 * N -                                        15)) / 10);      // Condition to check if N is a     // centered pentagonal number     return (n - (int)n) == 0; }  // Driver Code public static void main(String[] args) {          // Given Number     int N = 6;      // Function call     if (isCenteredpentagonal(N))     {         System.out.print("Yes");     }     else      {         System.out.print("No");     } } }  // This code is contributed by sapnasingh4991 
Python3
# Python3 program for the above approach  import numpy as np  # Function to check if number N  # is a centered pentagonal number  def isCenteredpentagonal(N):      n = (5 + np.sqrt(40 * N - 15)) / 10      # Condition to check if N is a      # centered pentagonal number      return (n - int(n)) == 0  # Driver Code  N = 6  # Function call  if (isCenteredpentagonal(N)):     print ("Yes") else:      print ("No")   # This code is contributed by PratikBasu 
C#
// C# program for the above approach using System;  class GFG{  // Function to check if number N // is a centered pentagonal number static bool isCenteredpentagonal(int N) {     float n = (float) ((5 + Math.Sqrt(40 * N -                                        15)) / 10);      // Condition to check if N is a     // centered pentagonal number     return (n - (int)n) == 0; }  // Driver Code public static void Main(string[] args) {          // Given number     int N = 6;      // Function call     if (isCenteredpentagonal(N))     {         Console.Write("Yes");     }     else     {         Console.Write("No");     } } }  // This code is contributed by rutvik_56 
JavaScript
<script> // javascript program for the above approach  // Function to check if number N // is a centered pentagonal number function isCenteredpentagonal(N) {     var n =  ((5 + Math.sqrt(40 * N -                                        15)) / 10);      // Condition to check if N is a     // centered pentagonal number     return (n - parseInt(n) == 0); }  // Driver Code //Given Number var N = 6;  // Function call if (isCenteredpentagonal(N)) {     document.write("Yes"); } else  {     document.write("No"); }  // This code is contributed by Amit Katiyar  </script> 

Output: 
Yes

 

Time Complexity: O(logN) because it is using inbuilt sqrt function
Auxiliary Space: O(1)


Next Article
Program to check if N is a Centered Octagonal Number
author
spp____
Improve
Article Tags :
  • Mathematical
  • DSA
Practice Tags :
  • Mathematical

Similar Reads

  • Program to check if N is a Centered Pentadecagonal Number
    Given a number N, the task is to check whether N is a Centered pentadecagonal number or not. If the number N is a Centered Pentadecagonal Number then print "Yes" else print "No". Centered Pentadecagonal Number represents a dot in the centre and other dots surrounding it in successive Pentadecagonal(
    4 min read
  • Program to check if N is a Centered Octagonal Number
    Given an integer N, the task is to check if it is a Centered Octagonal number or not. If the number N is an Centered Octagonal Number then print "Yes" else print "No". Centered Octagonal number represents an octagon with a dot in the centre and others dots surrounding the centre dot in the successiv
    4 min read
  • Program to check if N is a Centered Octadecagonal number
    Given an integer N, the task is to check if it is a Centered Octadecagonal number or not. Print "yes" if it is otherwise output is "no". Centered Octadecagonal number represents a dot in the centre and others dot are arranged around it in successive layers of octadecagon(18 sided polygon). The first
    4 min read
  • Program to check if N is a Pentagonal Number
    Given a number (N), check if it is pentagonal or not. Examples : Input: 12 Output: YesExplanation: 12 is the third pentagonal number Input: 19Output: NoExplanation: The third pentagonal number is 12 while the fourth pentagonal number is 22.Hence 19 is not a pentagonal number. Pentagonal numbers are
    14 min read
  • Program to check if N is a Octagonal Number
    Given a number N, the task is to check if N is an Octagonal Number or not. If the number N is an Octagonal Number then print "Yes" else print "No". Octagonal Number is the figure number that represent octagonal. Octagonal Numbers can be formed by placing triangular numbers on the four sides of a squ
    4 min read
  • Program to check if N is a Pentadecagonal Number
    Given a number N, the task is to check if N is a Pentadecagon Number or not. If the number N is an Pentadecagon Number then print "Yes" else print "No". Pentadecagon Number is a 15-sided polygon..The first few Pentadecagon numbers are 1, 15, 42, 82, 135, 201, ... Examples: Input: N = 15 Output: Yes
    4 min read
  • Program to check if N is a Centered nonadecagonal number
    Given an integer N, the task is to check if it is a Centered nonadecagonal number or not. Centered nonadecagonal number represents a dot in the centre and other dots surrounding it in successive nonadecagon(19 sided polygon) layers.The first few Centered nonadecagonal numbers are 1, 20, 58, 115, 191
    4 min read
  • Program to check if N is a Centered Cubic Number
    Given a number N, the task is to check if N is a centered cubic number or not. A centered cubic number counts the number of points which are formed by a point that is surrounded by concentric cubical layers in 3D with i2 points on the square faces of the i-th layer. The first few Centered cube numbe
    7 min read
  • Program to check if N is a Octadecagon number
    Given a number N, the task is to check if N is an Octadecagon Number or not. If the number N is an Octadecagon Number then print "Yes" else print "No". Octadecagon Number is a 18-sided polygon. The first few Octadecagon Numbers are 1, 18, 51, 100, 165, 246, 343, ... Examples: Input: N = 18 Output: Y
    4 min read
  • Program to check if N is a Centered Decagonal Number
    Given an integer N, the task is to check if N is a Centered Decagonal Number or not. If the number N is a Centered Decagonal Number then print "Yes" else print "No". Centered Decagonal Number is centered figurative number that represents a decagon with dot in center and all other dot surrounding it
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences