Skip to content
Related Articles

Related Articles

Improve Article

Program to check if N is a Octagonal Number

  • Last Updated : 23 Jun, 2021

Given a number N, the task is to check if N is a 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 square. The first few Octagonal Numbers are 1, 8, 21, 40, 65, 96 … 
 

Examples: 
 

Input: N = 8 
Output: Yes 
Explanation: 
Second octagonal number is 8.
Input: N = 30 
Output: No 
 

 



Approach: 
 

  1. The Kth term of the octagonal number is given as
    K^{th} Term = 3*K^{2} - 2*K
     
  2. As we have to check that the given number can be expressed as a Octagonal Number or not. This can be checked as: 
     

=> N = 3*K^{2} - 2*K
=> K = \frac{2 + \sqrt{12*N + 4}}{6}
 

  1.  
  2. If the value of K calculated using the above formula is an integer, then N is a Octagonal Number.
  3. Else N is not a Octagonal 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
// Octagonal Number
bool isoctagonal(int N)
{
    float n
        = (2 + sqrt(12 * N + 4))
          / 6;
 
    // Condition to check if the
    // number is a octagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 8;
 
    // Function call
    if (isoctagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function to check if N is a
// octagonal number
public static boolean isoctagonal(int N)
{
    double n = (2 + Math.sqrt(12 * N + 4)) / 6;
     
    // Condition to check if the
    // number is a octagonal number
    return (n - (int)n) == 0;
}
     
// Driver code
public static void main(String[] args)
{
     
    // Given Number
    int N = 8;
     
    // Function call
    if (isoctagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by coder001

Python3




# Python3 program for the above approach
from math import sqrt
 
# Function to check if N is a
# octagonal number
def isoctagonal(N):
 
    n = (2 + sqrt(12 * N + 4)) / 6;
 
    # Condition to check if the
    # number is a octagonal number
    return (n - int(n)) == 0;
     
# Driver Code
if __name__ == "__main__":
 
    # Given number
    N = 8;
 
    # Function call
    if (isoctagonal(N)):
        print("Yes");
     
    else:
        print("No");
 
# This code is contributed by AnkitRai01

C#




// C# program for the above approach
using System;
 
class GFG {
     
// Function to check if N is a
// octagonal number
public static bool isoctagonal(int N)
{
    double n = (2 + Math.Sqrt(12 * N +
                              4)) / 6;
     
    // Condition to check if the
    // number is a octagonal number
    return (n - (int)n) == 0;
}
     
// Driver code
public static void Main(String[] args)
{
     
    // Given number
    int N = 8;
     
    // Function call
    if (isoctagonal(N))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by Rohit_ranjan

Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to check if N is a
// Octagonal Number
function isoctagonal(N)
{
    var n = (2 + Math.sqrt(12 * N + 4)) / 6;
 
    // Condition to check if the
    // number is a octagonal number
    return (n - parseInt(n) == 0);
}
 
// Given Number
var N = 8;
 
// Function call
if (isoctagonal(N)) {
    document.write("Yes");
}
else {
    document.write("No");
}
 
</script>
Output: 
Yes

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :