Program to check if N is a Heptagonal Number
Last Updated :
05 Dec, 2023
Given an integer N, the task is to check if N is a Heptagonal Number or not. If the number N is an Heptagonal Number then print “Yes” else print “No”.
Heptagonal Number represents Heptagon and belongs to a figurative number. Heptagonal has seven angles, seven vertices, and seven-sided polygon. The first few Heptagonal Numbers are 1, 7, 18, 34, 55, 81, …
Examples:
Input: N = 7
Output: Yes
Explanation:
Second heptagonal number is 7.
Input: N = 30
Output: No
Approach:
- The Kth term of the heptagonal number is given as
- As we have to check that the given number can be expressed as a Heptagonal Number or not. This can be checked as:
=>
=>
- If the value of K calculated using the above formula is an integer, then N is a Heptagonal Number.
- Else N is not a Heptagonal Number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isheptagonal( int N)
{
float n
= (3 + sqrt (40 * N + 9))
/ 10;
return (n - ( int )n) == 0;
}
int main()
{
int N = 7;
if (isheptagonal(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static boolean isheptagonal( int N)
{
double n = ( 3 + Math.sqrt( 40 * N + 9 )) / 10 ;
return (n - ( int )n) == 0 ;
}
public static void main(String[] args)
{
int N = 7 ;
if (isheptagonal(N))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def isheptagonal(N):
n = ( 3 + math.sqrt( 40 * N + 9 )) / 10
return (n - int (n)) = = 0
N = 7
if (isheptagonal(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
public static bool isheptagonal( int N)
{
double n = (3 + Math.Sqrt(40 * N + 9)) / 10;
return (n - ( int )n) == 0;
}
public static void Main(String[] args)
{
int N = 7;
if (isheptagonal(N))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function isheptagonal(N)
{
var n = (3 + Math.sqrt(40 * N + 9))
/ 10;
return (n - parseInt(n)) == 0;
}
var N = 7;
if (isheptagonal(N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time complexity: O(logN) because it is using inbuilt sqrt function
Auxiliary Space: O(1)
Approach: Binary Search
Binary search to find if the given number N matches any Heptagonal Number. This approach is more efficient than brute
force as it reduces the search space by half at each iteration.
Below is code implementation of the above approach:
C++
#include <iostream>
using namespace std;
string isHeptagonal( int n) {
int left = 1;
int right = n;
while (left <= right) {
int mid = (left + right) / 2;
int hn = mid * (5 * mid - 3) / 2;
if (hn == n) {
return "Yes" ;
} else if (hn < n) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return "No" ;
}
int main() {
cout << isHeptagonal(7) << endl;
return 0;
}
|
Java
public class GFG {
static String isHeptagonal( int n) {
int left = 1 ;
int right = n;
while (left <= right) {
int mid = (left + right) / 2 ;
int hn = mid * ( 5 * mid - 3 ) / 2 ;
if (hn == n) {
return "Yes" ;
} else if (hn < n) {
left = mid + 1 ;
} else {
right = mid - 1 ;
}
}
return "No" ;
}
public static void main(String[] args) {
System.out.println(isHeptagonal( 7 ));
}
}
|
Python
def is_heptagonal(n):
left = 1
right = n
while left < = right:
mid = (left + right) / / 2
hn = mid * ( 5 * mid - 3 ) / / 2
if hn = = n:
return "Yes"
elif hn < n:
left = mid + 1
else :
right = mid - 1
return "No"
print (is_heptagonal( 7 ))
|
C#
using System;
class Program
{
static string IsHeptagonal( int n)
{
int left = 1;
int right = n;
while (left <= right)
{
int mid = (left + right) / 2;
int hn = mid * (5 * mid - 3) / 2;
if (hn == n)
{
return "Yes" ;
}
else if (hn < n)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return "No" ;
}
static void Main()
{
Console.WriteLine(IsHeptagonal(7));
}
}
|
Javascript
function isHeptagonal(n) {
let left = 1;
let right = n;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
let hn = mid * (5 * mid - 3) / 2;
if (hn === n) {
return "Yes" ;
} else if (hn < n) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return "No" ;
}
console.log(isHeptagonal(7));
|
Time Complexity: O(log n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...