Program to check if N is a Nonagonal Number
Given a number N, the task is to check if N is a Nonagonal Number or not. If the number N is an Nonagonal Number then print “Yes” else print “No”.
Nonagonal Number is a figurate number that extends the concept of triangular and square numbers to the Nonagon. Specifically, the nth Nonagonal Numbers count the number of dots in a pattern of n nested nonagons(9 sided polygon), all sharing a common corner, where the ith nonagon in the pattern has sides made of i dots spaced one unit apart from each other. The first few Nonagonal Numbers are 1, 9, 24, 46, 75, 111, 154, …
Examples:
Input: N = 9
Output: Yes
Explanation:
Second Nonagonal Number is 9.
Input: N = 20
Output: No
Approach:
1. The Kth term of the nonagonal number is given as
2. As we have to check that the given number can be expressed as a Nonagonal Number or not. This can be checked as:
=>
=>
3. If the value of K calculated using the above formula is an integer, then N is a Nonagonal Number.
4. Else N is not a Nonagonal 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 N is a // is a Nonagonal Number bool isnonagonal( int N) { float n = (5 + sqrt (56 * N + 25)) / 14; // Condition to check if the // number is a nonagonal number return (n - ( int )n) == 0; } // Driver Code int main() { // Given Number int N = 9; // Function call if (isnonagonal(N)) { cout << "Yes" ; } else { cout << "No" ; } return 0; } |
Java
// Java program for the above approach import java.lang.Math; class GFG{ // Function to check if N is a // nonagonal number public static boolean isnonagonal( int N) { double n = ( 5 + Math.sqrt( 56 * N + 25 )) / 14 ; // Condition to check if the // number is a nonagonal number return (n - ( int )n) == 0 ; } // Driver code public static void main(String[] args) { // Given number int N = 9 ; // Function call if (isnonagonal(N)) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } // This code is contributed by divyeshrabadiya07 |
Python3
# Python3 program for the above approach # Function to check if N is a # nonagonal number def isnonagonal(N): n = ( 5 + pow (( 56 * N + 25 ), 1 / 2 )) / 14 ; # Condition to check if the # number is a nonagonal number return (n - int (n)) = = 0 ; # Driver code if __name__ = = '__main__' : # Given number N = 9 ; # Function call if (isnonagonal(N)): print ( "Yes" ); else : print ( "No" ); # This code is contributed by Rajput-Ji |
C#
// C# program for the above approach using System; class GFG{ // Function to check if N is a // nonagonal number public static bool isnonagonal( int N) { double n = (5 + Math.Sqrt(56 * N + 25)) / 14; // Condition to check if the // number is a nonagonal number return (n - ( int )n) == 0; } // Driver code public static void Main( string [] args) { // Given number int N = 9; // Function call if (isnonagonal(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 N is a // is a Nonagonal Number function isnonagonal(N) { let n = (5 + Math.sqrt(56 * N + 25)) / 14; // Condition to check if the // number is a nonagonal number return (n - parseInt(n)) == 0; } // Driver Code // Given Number let N = 9; // Function call if (isnonagonal(N)) { document.write( "Yes" ); } else { document.write( "No" ); } // This code is contributed by subhammahato348. </script> |
Output:
Yes
Time Complexity: O(log N) because sqrt() function is being used
Auxiliary Space: O(1)