Open In App

Program to check if N is a Centered Pentadecagonal Number

Last Updated : 19 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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(15-sided polygon) layers. The first few Centered pentadecagonal numbers are 1, 16, 46 … 
 

Examples:  

Input: N = 16 
Output: Yes 
Explanation: 
Second Centered pentadecagonal number is 16.


Input: N = 20 
Output: No  

Approach:  

1. The Kth term of the Centered pentadecagonal number is given as:
K^{th} Term = \frac{15*K^{2} - 15*K + 2}{2}
 

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

=> N = \frac{15*K^{2} - 15*K + 2}{2}
=> K = \frac{16 + \sqrt{120*N + 105}}{30}

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

4. Else the number N is not a Centered Pentadecagonal 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 Pentadecagonal Number
bool isCenteredpentadecagonal(int N)
{
    float n
        = (16 + sqrt(120 * N + 105))
          / 30;
 
    // Condition to check if N is a
    // Centered Pentadecagonal Number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 16;
 
    // Function call
    if (isCenteredpentadecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

                    

Java

// Java program for the above approach
class GFG{
 
// Function to check if number N is a
// Centered Pentadecagonal Number
static boolean isCenteredpentadecagonal(int N)
{
    float n = (float)(16 + Math.sqrt(120 * N +
                                     105)) / 30;
 
    // Condition to check if N is a
    // Centered Pentadecagonal Number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Number
    int N = 16;
 
    // Function call
    if (isCenteredpentadecagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by rutvik_56

                    

Python3

# Python3 program for the above approach
import math
 
# Function to check if number N is a
# centered pentadecagonal number
def isCenteredpentadecagonal(N):
     
    n = (16 + math.sqrt(120 * N + 105)) / 30
     
    # Condition to check if N is a
    # centered pentadecagonal number
    return (n - int(n)) == 0
 
# Driver Code
N = 16
 
# Function call
if isCenteredpentadecagonal(N):
    print("Yes")
else :
    print("No")
 
# This code is contributed by ishayadav181

                    

C#

// C# program for the above approach
using System;
class GFG{
     
// Function to check if number N is a
// centered pentadecagonal number
public static bool isCenteredpentadecagonal(int N)
{
    double n = (16 + Math.Sqrt(120 * N +
                               105)) / 30;
     
    // Condition to check if N is a
    // centered pentadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver code
public static void Main()
{
     
    // Given number
    int N = 16;
     
    // Function call
    if (isCenteredpentadecagonal(N))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07

                    

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to check if number N is a
// Centered Pentadecagonal Number
function isCenteredpentadecagonal(N)
{
    var n = (16 + Math.sqrt(120 * N +
                          105)) / 30;
 
    // Condition to check if N is a
    // Centered Pentadecagonal Number
    return (n - (parseInt(n))) == 0;
}
 
// Driver Code
 
// Given Number
var N = 16;
 
// Function call
if (isCenteredpentadecagonal(N))
{
    document.write("Yes");
}
else
{
    document.write("No");
}
 
// This code is contributed by Kirti
     
</script>

                    

Output: 
No

 

Time Complexity: O(logN) since inbuilt sqrt function has been used

Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads