Check if a number is power of 8 or not
Last Updated :
21 Jun, 2022
Given a number check whether it is a power of 8 or not.
Examples :
Input : n = 64
Output : Yes
Input : 75
Output : No
First solution
We calculate log8(n) of the number if it is an integer, then n is in the power of 8. We use trunc(n) function that finds the closest integer for a double value.
C++
#include <cmath>
#include <iostream>
using namespace std;
bool checkPowerof8( int n)
{
double i = log (n) / log (8);
return (i - trunc(i) < 0.000001);
}
int main()
{
int n = 65;
checkPowerof8(n) ? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
class GFG {
static boolean checkPowerof8( int n)
{
double i = Math.log(n) / Math.log( 8 );
return (i - Math.floor(i) < 0.000001 );
}
public static void main(String args[])
{
int n = 65 ;
if (checkPowerof8(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
from math import log,trunc
def checkPowerof8(n):
i = log(n, 8 )
return (i - trunc(i) < 0.000001 );
n = 65
if checkPowerof8(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool checkPowerof8( int n)
{
double i = Math.Log(n) / Math.Log(8);
return (i - Math.Floor(i) < 0.000001);
}
static public void Main()
{
int n = 65;
if (checkPowerof8(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function checkPowerof8( $n )
{
$i = log( $n ) / log(8);
return ( $i - floor ( $i ) < 0.000001);
}
$n = 65;
if (checkPowerof8( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function checkPowerof8(n)
{
let i = Math.log(n) / Math.log(8);
return (i - Math.floor(i) < 0.000001);
}
let n = 65;
if (checkPowerof8(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output :
No
Time Complexity: O(1)
Auxiliary Space: O(1)
Second solution
A number is a power of 8 if the following conditions are satisfied.
- The number is the power of two. A number is the power of two if it has only one set bit, i.e., bitwise and of n and n-1 is 0.
- The number has its only set a bit at position 0 or 3 or 6 or …. 30 [For a 32-bit number]. To check the position of its set bit we can use a mask (0xB6DB6DB6)16 = (10110110110110110110110110110110)2.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
bool checkPowerof8( int n)
{
return (n && !(n & (n - 1)) && !(n & 0xB6DB6DB6));
}
int main()
{
int n = 65;
checkPowerof8(n) ? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean checkPowerof8( int n)
{
return (n > 0 && (n & (n - 1 )) > 0 &&
(n & 0xB6DB6DB6 ) > 0 );
}
public static void main(String[] args)
{
int n = 65 ;
if (checkPowerof8(n) == true )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def checkPowerof8(n):
return (n and not (n & (n - 1 )) and
not (n & 0xB6DB6DB6 ))
if __name__ = = "__main__" :
n = 65
if checkPowerof8(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool checkPowerof8( int n)
{
return (n > 0 && (n & (n - 1)) > 0 &&
(n & 0xB6DB6DB6) > 0);
}
static public void Main()
{
int n = 65;
if (checkPowerof8(n) == true )
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
function checkPowerof8( $n )
{
$t = ( $n && !( $n & ( $n - 1)) &&
!( $n & 0xB6DB6DB6));
return $t ;
}
$n = 65;
if (checkPowerof8( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function checkPowerof8( n)
{
return (n && !(n & (n - 1)) && !(n & 0xB6DB6DB6));
}
var n = 65;
if (checkPowerof8(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output :
No
Time Complexity: O(1)
Auxiliary Space: O(1)
One simple observation that can be made here is that if a number is the power of 8 then it has only a one-bit set and that bit is at positions 1, 4, 7, 10, …
Thus, we can just check if the only bit set in the number is at one of these positions then it is a power of 8 otherwise not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkPowerof8( int n)
{
int i = 0;
unsigned long long l = 1;
while (i <= 63) {
l <<= i;
if (l == n)
return true ;
i += 3;
l = 1;
}
return false ;
}
int main()
{
int n = 65;
if (checkPowerof8(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean checkPowerof8( int n)
{
int i = 0 ;
long l = 1 ;
while (i <= 63 )
{
l <<= i;
if (l == n)
return true ;
i += 3 ;
l = 1 ;
}
return false ;
}
public static void main (String[] args)
{
int n = 65 ;
if (checkPowerof8(n))
System.out.println ( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkPowerof8(n):
i = 0
l = 1
while (i < = 63 ):
l << = i
if (l = = n):
return True
i + = 3
l = 1
return False
if __name__ = = '__main__' :
n = 65
if (checkPowerof8(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool checkPowerof8( int n)
{
int i = 0;
long l = 1;
while (i <= 63)
{
l <<= i;
if (l == n)
return true ;
i += 3;
l = 1;
}
return false ;
}
static public void Main ()
{
int n = 65;
if (checkPowerof8(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function checkPowerof8( n)
{
var i = 0;
var l= 1;
while (i <= 63) {
l<<=i;
if (l == n)
return 1;
i += 3;
l = 1;
}
return 0;
}
var n = 65;
if (checkPowerof8(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output :
No
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...