Given a number n, check it is the Stella Octangula number or not. A number of the form where n is a whole number(0, 1, 2, 3, 4, …) is called Stella Octangula. First few Stella Octangula numbers are 0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 1990, …
Stella octangula numbers which are perfect squares are 1 and 9653449.
Given a number x, check if it is Stella octangula.
Examples:
Input: x = 51
Output: Yes
Explanation: For n = 3, the value of expression n(2n2 – 1) is 51
Input: n = 53
Output: No
A simple solution is to run a loop starting from n = 0. For every n, check if n(2n2 – 1) is equal to x. We run the loop while value of n(2n2 – 1) is smaller than or equal to x.
An efficient solution is to use Unbounded Binary Search. We first find a value of n such that n(2n2 – 1) is greater than x using repeated doubling. Then we apply Binary Search.
C++
#include <bits/stdc++.h>
using namespace std;
int f( int n) {
return n*(2*n*n - 1);
}
bool binarySearch( int low, int high, int x)
{
while (low <= high) {
long long mid = (low + high) / 2;
if (f(mid) < x)
low = mid + 1;
else if (f(mid) > x)
high = mid - 1;
else
return true ;
}
return false ;
}
bool isStellaOctangula( int x)
{
if (x == 0)
return true ;
int i = 1;
while (f(i) < x)
i = i*2;
if (f(i) == x)
return true ;
return binarySearch(i/2, i, x);
}
int main()
{
int n = 51;
if (isStellaOctangula(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int f( int n)
{
return n * ( 2 * n *
n - 1 );
}
static boolean binarySearch( int low,
int high,
int x)
{
while (low <= high)
{
int mid = (low + high) / 2 ;
if (f(mid) < x)
low = mid + 1 ;
else if (f(mid) > x)
high = mid - 1 ;
else
return true ;
}
return false ;
}
static boolean isStellaOctangula( int x)
{
if (x == 0 )
return true ;
int i = 1 ;
while (f(i) < x)
i = i * 2 ;
if (f(i) == x)
return true ;
return binarySearch(i / 2 ,
i, x);
}
public static void main (String[] args)
{
int n = 51 ;
if (isStellaOctangula(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def f(n):
return n * ( 2 * n * n - 1 );
def binarySearch(low, high, x):
while (low < = high):
mid = int ((low + high) / / 2 );
if (f(mid) < x):
low = mid + 1 ;
elif (f(mid) > x):
high = mid - 1 ;
else :
return True ;
return False ;
def isStellaOctangula(x):
if (x = = 0 ):
return True ;
i = 1 ;
while (f(i) < x):
i = i * 2 ;
if (f(i) = = x):
return True ;
return binarySearch(i / 2 , i, x);
n = 51 ;
if (isStellaOctangula(n) = = True ):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static int f( int n)
{
return n * (2 * n *
n - 1);
}
static bool binarySearch( int low,
int high,
int x)
{
while (low <= high)
{
int mid = (low + high) / 2;
if (f(mid) < x)
low = mid + 1;
else if (f(mid) > x)
high = mid - 1;
else
return true ;
}
return false ;
}
static bool isStellaOctangula( int x)
{
if (x == 0)
return true ;
int i = 1;
while (f(i) < x)
i = i * 2;
if (f(i) == x)
return true ;
return binarySearch(i / 2,
i, x);
}
public static void Main ()
{
int n = 51;
if (isStellaOctangula(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function f(n)
{
return n * (2 * n *
n - 1);
}
function binarySearch(low, high, x)
{
while (low <= high)
{
let mid = (low + high) / 2;
if (f(mid) < x)
low = mid + 1;
else if (f(mid) > x)
high = mid - 1;
else
return true ;
}
return false ;
}
function isStellaOctangula(x)
{
if (x == 0)
return true ;
let i = 1;
while (f(i) < x)
i = i * 2;
if (f(i) == x)
return true ;
return binarySearch(i / 2,
i, x);
}
let n = 51;
if (isStellaOctangula(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(log n)
Auxiliary Space: O(1)
Last Updated :
10 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...