Given an integer n, find whether it is a power of 4 or not.
Example :
Input : 16
Output : 16 is a power of 4
Input : 20
Output : 20 is not a power of 4
Method 1: Using the in-built log method: Take log of given number to the base 4, and then raise 4 to this result. If the output is equal to n, then given number is a power of 4, else not.
C++
#include<bits/stdc++.h>
using namespace std;
class GFG
{
public : bool isPowerOfFour( int n)
{
if (n == pow (4, ( int )( log (n)/ log (4))) && n != 0)
return true ;
return false ;
}
};
int main()
{
GFG g;
int test_no = 64;
if (g.isPowerOfFour(test_no))
cout << test_no << " is a power of 4" ;
else
cout << test_no << " is not a power of 4" ;
}
|
Java
import java.io.*;
class GFG {
public boolean isPowerOfFour( int n)
{
if (n != 0 && n == ( int )Math.pow( 4 , (Math.log(n) / Math.log( 4 )))) {
return true ;
}
return false ;
}
public static void main(String[] args)
{
GFG g = new GFG();
int test_no = 64 ;
if (g.isPowerOfFour(test_no)) {
System.out.print(test_no + " is a power of 4 " );
}
else {
System.out.print(test_no
+ " is not a power of 4" );
}
}
}
|
C#
using System;
public class GFG {
public bool isPowerOfFour( int n)
{
if (n != 0 && n == Math.Pow(4, (Math.Log(n) / Math.Log(4)))) {
return true ;
}
return false ;
}
static public void Main()
{
GFG g = new GFG();
int test_no = 64;
if (g.isPowerOfFour(test_no)) {
Console.Write(test_no + " is a power of 4" );
}
else {
Console.Write(test_no + " is not a power of 4" );
}
}
}
|
Python3
import math
def isPowerOfFour(n):
if (n ! = 0 and n = = pow ( 4 , (math.log(n) / math.log( 4 )))):
return True
return False
test_no = 64
if (isPowerOfFour(test_no)):
print (test_no, ' is a power of 4' )
else :
print (test_no, ' is not a power of 4' )
|
Javascript
<script>
public bool isPowerOfFour(int n)
{
if (n != 0 && n == Math.Pow(4, (Math.Log(n) / Math.Log(4)))) {
return true ;
}
return false ;
}
static public void Main()
{
GFG g = new GFG();
int test_no = 64;
if (g.isPowerOfFour(test_no)) {
document.write(test_no + " is a power of 4" );
}
else {
document.write(test_no + " is not a power of 4" );
}
</script>
|
Output
64 is a power of 4
Time Complexity: O(log(log2(n))*log2(n))
Auxiliary Space: O(1)
Method 2: Another solution is to keep dividing the number by 4, i.e, do n = n/4 iteratively. In any iteration, if n%4 becomes non-zero and n is not 1 then n is not a power of 4, otherwise, n is a power of 4.
C++
#include<iostream>
using namespace std;
#define bool int
class GFG
{
public : bool isPowerOfFour( int n)
{
if (n == 0)
return 0;
while (n != 1)
{
if (n % 4 != 0)
return 0;
n = n / 4;
}
return 1;
}
};
int main()
{
GFG g;
int test_no = 64;
if (g.isPowerOfFour(test_no))
cout << test_no << " is a power of 4" ;
else
cout << test_no << "is not a power of 4" ;
getchar ();
}
|
C
#include<stdio.h>
#define bool int
bool isPowerOfFour( int n)
{
if (n == 0)
return 0;
while (n != 1)
{
if (n % 4 != 0)
return 0;
n = n / 4;
}
return 1;
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
printf ( "%d is a power of 4" , test_no);
else
printf ( "%d is not a power of 4" , test_no);
getchar ();
}
|
Java
class GFG {
static int isPowerOfFour( int n)
{
if (n == 0 )
return 0 ;
while (n != 1 )
{
if (n % 4 != 0 )
return 0 ;
n = n / 4 ;
}
return 1 ;
}
public static void main(String[] args)
{
int test_no = 64 ;
if (isPowerOfFour(test_no) == 1 )
System.out.println(test_no +
" is a power of 4" );
else
System.out.println(test_no +
"is not a power of 4" );
}
}
|
Python3
def isPowerOfFour(n):
if (n = = 0 ):
return False
while (n ! = 1 ):
if (n % 4 ! = 0 ):
return False
n = n / / 4
return True
test_no = 64
if (isPowerOfFour( 64 )):
print (test_no, 'is a power of 4' )
else :
print (test_no, 'is not a power of 4' )
|
C#
using System;
class GFG {
static int isPowerOfFour( int n)
{
if (n == 0)
return 0;
while (n != 1) {
if (n % 4 != 0)
return 0;
n = n / 4;
}
return 1;
}
public static void Main()
{
int test_no = 64;
if (isPowerOfFour(test_no) == 1)
Console.Write(test_no +
" is a power of 4" );
else
Console.Write(test_no +
" is not a power of 4" );
}
}
|
PHP
<?php
function isPowerOfFour( $n )
{
if ( $n == 0)
return 0;
while ( $n != 1)
{
if ( $n % 4 != 0)
return 0;
$n = $n / 4;
}
return 1;
}
$test_no = 64;
if (isPowerOfFour( $test_no ))
echo $test_no , " is a power of 4" ;
else
echo $test_no , " is not a power of 4" ;
?>
|
Javascript
<script>
function isPowerOfFour( n)
{
if (n == 0)
return false ;
while (n != 1)
{
if (n % 4 != 0)
return false ;
n = n / 4;
}
return true ;
}
let test_no = 64;
if (isPowerOfFour(test_no))
document.write(test_no+ " is a power of 4" );
else
document.write(test_no+ " is not a power of 4" );
</script>
|
Output
64 is a power of 4
Time Complexity: O(log4n)
Auxiliary Space: O(1)
Method 3: A number n is a power of 4 if the following conditions are met.
a) There is only one bit set in the binary representation of n (or n is a power of 2)
b) The count of zero bits before the (only) set bit is even.
For example 16 (10000) is the power of 4 because there is only one bit set and count of 0s before the set bit is 4 which is even.
Thanks to Geek4u for suggesting the approach and providing the code.
C++
#include<bits/stdc++.h>
using namespace std;
bool isPowerOfFour(unsigned int n)
{
int count = 0;
if ( n && !(n&(n-1)) )
{
while (n > 1)
{
n >>= 1;
count += 1;
}
return (count%2 == 0)? 1 :0;
}
return 0;
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
cout << test_no << " is a power of 4" ;
else
cout << test_no << " is not a power of 4" ;
}
|
C
#include<stdio.h>
#define bool int
bool isPowerOfFour(unsigned int n)
{
int count = 0;
if ( n && !(n&(n-1)) )
{
while (n > 1)
{
n >>= 1;
count += 1;
}
return (count%2 == 0)? 1 :0;
}
return 0;
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
printf ( "%d is a power of 4" , test_no);
else
printf ( "%d is not a power of 4" , test_no);
getchar ();
}
|
Java
import java.io.*;
class GFG
{
static int isPowerOfFour( int n)
{
int count = 0 ;
int x = n & (n - 1 );
if ( n > 0 && x == 0 )
{
while (n > 1 )
{
n >>= 1 ;
count += 1 ;
}
return (count % 2 == 0 ) ? 1 : 0 ;
}
return 0 ;
}
public static void main(String[] args)
{
int test_no = 64 ;
if (isPowerOfFour(test_no)> 0 )
System.out.println(test_no +
" is a power of 4" );
else
System.out.println(test_no +
" is not a power of 4" );
}
}
|
Python3
def isPowerOfFour(n):
count = 0
if (n and ( not (n & (n - 1 )))):
while (n > 1 ):
n >> = 1
count + = 1
if (count % 2 = = 0 ):
return True
else :
return False
test_no = 64
if (isPowerOfFour( 64 )):
print (test_no, 'is a power of 4' )
else :
print (test_no, 'is not a power of 4' )
|
C#
using System;
class GFG {
static int isPowerOfFour( int n)
{
int count = 0;
int x = n & (n-1);
if ( n > 0 && x == 0)
{
while (n > 1)
{
n >>= 1;
count += 1;
}
return (count % 2 == 0) ? 1 : 0;
}
return 0;
}
static void Main()
{
int test_no = 64;
if (isPowerOfFour(test_no)>0)
Console.WriteLine( "{0} is a power of 4" ,
test_no);
else
Console.WriteLine( "{0} is not a power of 4" ,
test_no);
}
}
|
PHP
<?php
function isPowerOfFour( $n )
{
$count = 0;
if ( $n && !( $n &( $n -1)) )
{
while ( $n > 1)
{
$n >>= 1;
$count += 1;
}
return ( $count %2 == 0)? 1 :0;
}
return 0;
}
$test_no = 64;
if (isPowerOfFour( $test_no ))
echo $test_no , " is a power of 4" ;
else
echo $test_no , " not is a power of 4" ;
#This Code is Contributed by Ajit
?>
|
Javascript
<script>
function isPowerOfFour( n)
{
let count = 0;
if ( n && !(n&(n-1)) )
{
while (n > 1)
{
n >>= 1;
count += 1;
}
return (count%2 == 0)? 1 :0;
}
return 0;
}
let test_no = 64;
if (isPowerOfFour(test_no))
document.write( test_no + " is a power of 4" );
else
document.write(test_no + " is not a power of 4" );
</script>
|
Output
64 is a power of 4
Time Complexity: O(log4n)
Auxiliary Space: O(1)
Method 4: A number n is a power of 4 if the following conditions are met.
a) There is only one bit set in the binary representation of n (or n is a power of 2)
b) The bits don’t AND(&) any part of the pattern 0xAAAAAAAA
For example: 16 (10000) is power of 4 because there is only one bit set and 0x10 & 0xAAAAAAAA is zero.
Thanks to Sarthak Sahu for suggesting the approach.
C++
#include<bits/stdc++.h>
using namespace std;
bool isPowerOfFour(unsigned int n)
{
return n !=0 && ((n&(n-1)) == 0) && !(n & 0xAAAAAAAA);
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
cout << test_no << " is a power of 4" ;
else
cout << test_no << " is not a power of 4" ;
}
|
C
#include<stdio.h>
#define bool int
bool isPowerOfFour(unsigned int n)
{
return n != 0 && ((n&(n-1)) == 0) && !(n & 0xAAAAAAAA);
}
int main() {
int test_no = 64;
if (isPowerOfFour(test_no))
printf ( "%d is a power of 4" , test_no);
else
printf ( "%d is not a power of 4" , test_no);
getchar ();
}
|
Java
import java.io.*;
class GFG {
static boolean isPowerOfFour( int n) {
return n != 0 && ((n&(n- 1 )) == 0 ) && (n & 0xAAAAAAAA ) == 0 ;
}
public static void main(String[] args) {
int test_no = 64 ;
if (isPowerOfFour(test_no))
System.out.println(test_no +
" is a power of 4" );
else
System.out.println(test_no +
" is not a power of 4" );
}
}
|
Python3
def isPowerOfFour(n):
return (n ! = 0 and
((n & (n - 1 )) = = 0 ) and
not (n & 0xAAAAAAAA ));
test_no = 64 ;
if (isPowerOfFour(test_no)):
print (test_no , "is a power of 4" );
else :
print (test_no , "is not a power of 4" );
|
C#
using System;
class GFG
{
static bool isPowerOfFour( int n)
{
return n != 0 && ((n&(n-1)) == 0) &&
(n & 0xAAAAAAAA) == 0;
}
static void Main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
Console.WriteLine( "{0} is a power of 4" ,
test_no);
else
Console.WriteLine( "{0} is not a power of 4" ,
test_no);
}
}
|
Javascript
<script>
function isPowerOfFour( n)
{
return n !=0 && ((n&(n-1)) == 0) && !(n & 0xAAAAAAAA);
}
test_no = 64;
if (isPowerOfFour(test_no))
document.write(test_no + " is a power of 4" );
else
document.write(test_no + " is not a power of 4" );
</script>
|
Output
64 is a power of 4
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 5: A number will be a power of 4 if the floor(log4(num))=ceil(log4(num) because log4 of a number that is a power of 4 will always be an integer.
Below is the implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
float logn( int n, int r)
{
return log (n) / log (r);
}
bool isPowerOfFour( int n)
{
if (n == 0)
return false ;
return floor (logn(n,4))== ceil (logn(n,4));
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
cout << test_no << " is a power of 4" ;
else
cout << test_no << " is not a power of 4" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static double logn( int n,
int r)
{
return Math.log(n) /
Math.log(r);
}
static boolean isPowerOfFour( int n)
{
if (n == 0 )
return false ;
return Math.floor(logn(n, 4 )) ==
Math.ceil(logn(n, 4 ));
}
public static void main(String[] args)
{
int test_no = 64 ;
if (isPowerOfFour(test_no))
System.out.print(test_no +
" is a power of 4" );
else
System.out.print(test_no +
" is not a power of 4" );
}
}
|
Python3
import math
def logn(n, r):
return math.log(n) / math.log(r)
def isPowerOfFour(n):
if (n = = 0 ):
return False
return (math.floor(logn(n, 4 )) = =
math.ceil(logn(n, 4 )))
if __name__ = = '__main__' :
test_no = 64
if (isPowerOfFour(test_no)):
print (test_no, " is a power of 4" )
else :
print (test_no, " is not a power of 4" )
|
C#
using System;
class GFG{
static double logn( int n,
int r)
{
return Math.Log(n) /
Math.Log(r);
}
static bool isPowerOfFour( int n)
{
if (n == 0)
return false ;
return Math.Floor(logn(n, 4)) ==
Math.Ceiling(logn(n, 4));
}
public static void Main(String[] args)
{
int test_no = 64;
if (isPowerOfFour(test_no))
Console.Write(test_no +
" is a power of 4" );
else
Console.Write(test_no +
" is not a power of 4" );
}
}
|
Javascript
<script>
function logn( n, r)
{
return Math.log(n) / Math.log(r);
}
function isPowerOfFour( n)
{
if (n == 0)
return false ;
return Math.floor(logn(n,4))==Math.ceil(logn(n,4));
}
let test_no = 64;
if (isPowerOfFour(test_no))
document.write(test_no + " is a power of 4" ) ;
else
document.write( test_no + " is not a power of 4" );
</script>
|
Output
64 is a power of 4
Time Complexity: O(log n)
Auxiliary Space: O(1)
Method 6: Using Log and without using ceil (Oneliner)
We can easily calculate without using ceil by checking the log of number base 4 to the power of 4 is equal to that number
C++
#include <bits/stdc++.h>
using namespace std;
int isPowerOfFour( int n)
{
return (n > 0 and pow (4, int (log2(n) /
log2(4))) == n);
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
cout << test_no << " is a power of 4" ;
else
cout << test_no << " is not a power of 4" ;
return 0;
}
|
Java
import java.io.*;
class GFG{
static boolean isPowerOfFour( int n)
{
return (n > 0 && Math.pow(
4 , ( int )((Math.log(n) /
Math.log( 2 )) /
(Math.log( 4 ) /
Math.log( 2 )))) == n);
}
public static void main(String[] args)
{
int test_no = 64 ;
if (isPowerOfFour(test_no))
System.out.println(test_no +
" is a power of 4" );
else
System.out.println(test_no +
" is not a power of 4" );
}
}
|
Python3
import math
def isPowerOfFour(n):
return (n > 0 and 4 * * int (math.log(n, 4 )) = = n)
if __name__ = = '__main__' :
test_no = 64
if (isPowerOfFour(test_no)):
print (test_no, " is a power of 4" )
else :
print (test_no, " is not a power of 4" )
|
C#
using System;
class GFG
{
static Boolean isPowerOfFour( int n)
{
return (n > 0 && Math.Pow(
4, ( int )((Math.Log(n) /
Math.Log(2)) /
(Math.Log(4) /
Math.Log(2)))) == n);
}
public static void Main(String[] args)
{
int test_no = 64;
if (isPowerOfFour(test_no))
Console.WriteLine(test_no +
" is a power of 4" );
else
Console.WriteLine(test_no +
" is not a power of 4" );
}
}
|
Javascript
<script>
function isPowerOfFour(n)
{
return (n > 0 && Math.pow(4, (Math.log2(n) /
Math.log2(4)) == n));
}
let test_no = 64;
if (isPowerOfFour(test_no))
document.write(test_no +
" is a power of 4" );
else
document.write(test_no +
" is not a power of 4" );
</script>
|
Output
64 is a power of 4
Time Complexity: O(log4n)
Auxiliary Space: O(1)
Method 7: A number ‘n’ is a power of 4 if,
a) It is a perfect square
b) It is a power of two
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int n)
{
int x = sqrt (n);
return (x * x == n);
}
bool isPowerOfFour( int n)
{
if (n <= 0)
return false ;
if (!isPerfectSquare(n))
return false ;
return !(n & (n - 1));
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
cout << test_no << " is a power of 4" ;
else
cout << test_no << " is not a power of 4" ;
return 0;
}
|
C
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
bool isPerfectSquare( int n)
{
int x = sqrt (n);
return (x * x == n);
}
bool isPowerOfFour( int n)
{
if (n <= 0)
return false ;
if (!isPerfectSquare(n))
return false ;
return !(n & (n - 1));
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
printf ( "%d is a power of 4" , test_no);
else
printf ( "%d is not a power of 4" , test_no);
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean isPerfectSquare( int n) {
int x = ( int ) Math.sqrt(n);
return (x * x == n);
}
static boolean isPowerOfFour( int n)
{
if (n <= 0 )
return false ;
if (!isPerfectSquare(n))
return false ;
return (n & (n - 1 )) != 1 ? true : false ;
}
public static void main(String[] args) {
int test_no = 64 ;
if (isPowerOfFour(test_no))
System.out.print(test_no + " is a power of 4" );
else
System.out.print(test_no + " is not a power of 4" );
}
}
|
Python3
import math
def isPerfectSquare(n):
x = math.sqrt(n)
return (x * x = = n)
def isPowerOfFour(n):
if (n < = 0 ):
return False
if (isPerfectSquare(n)):
return False
return (n & (n - 1 ))
test_no = 64
if (isPowerOfFour(test_no)):
print (test_no , " is a power of 4" )
else :
print (test_no , " is not a power of 4" )
|
C#
using System;
public class GFG {
static bool isPerfectSquare( int n) {
int x = ( int ) Math.Sqrt(n);
return (x * x == n);
}
static bool isPowerOfFour( int n)
{
if (n <= 0)
return false ;
if (!isPerfectSquare(n))
return false ;
return (n & (n - 1)) != 1 ? true : false ;
}
public static void Main(String[] args) {
int test_no = 64;
if (isPowerOfFour(test_no))
Console.Write(test_no + " is a power of 4" );
else
Console.Write(test_no + " is not a power of 4" );
}
}
|
Javascript
<script>
function isPerfectSquare( n) {
var x = Math.sqrt(n);
return (x * x == n);
}
function isPowerOfFour(n)
{
if (n <= 0)
return false ;
if (!isPerfectSquare(n))
return false ;
return (n & (n - 1)) != 1 ? true : false ;
}
var test_no = 64;
if (isPowerOfFour(test_no))
document.write(test_no + " is a power of 4" );
else
document.write(test_no + " is not a power of 4" );
</script>
|
Output
64 is a power of 4
Time Complexity: O(log2n)
Auxiliary Space: O(1)
Method 8: Using a similar concept of “Power of 2”:
A number can be considered a power of 4 when:
- * The number is a power of 2 for example : 4, 16, 64 … all are powers of 2 as well . The O(1) bit manipulation technique using n&(n-1)==0 can be used, but wait every power of 4 is a power of 2. Will the reverse apply?
- *Obviously not, 8,32,128 … aren’t power of 4, so we need one more check here. If you clearly notice all powers of 4 when divided by 3 gives 1 as the remainder.
Below is the code implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPowerOfFour( int n)
{
return ((n > 0) && ((n & n - 1) == 0) && (n % 3 == 1));
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no) == true )
cout << (test_no) << " is a power of 4" ;
else
cout << (test_no) << " is not a power of 4" ;
}
|
Java
import java.io.*;
class GFG {
static boolean isPowerOfFour( int n)
{
return ((n> 0 ) && ((n & n- 1 ) == 0 ) && (n% 3 == 1 ));
}
public static void main(String[] args)
{
int test_no = 64 ;
if (isPowerOfFour(test_no) == true )
System.out.println(test_no +
" is a power of 4" );
else
System.out.println(test_no +
" is not a power of 4" );
}
}
|
Python3
def isPowerOfFour(n):
return ((n > 0 ) and ((n & n - 1 ) = = 0 ) and (n % 3 = = 1 ))
test_no = 64
if (isPowerOfFour(test_no)):
print (test_no, "is a power of 4" )
else :
print (test_no, "is not a power of 4" )
|
C#
using System;
class GFG
{
static bool isPowerOfFour( int n)
{
return ((n > 0) && ((n & n - 1) == 0)
&& (n % 3 == 1));
}
static void Main()
{
int test_no = 64;
if (isPowerOfFour(test_no) == true )
Console.Write(test_no + " is a power of 4" );
else
Console.Write(test_no + " is not a power of 4" );
}
}
|
Javascript
function isPowerOfFour(n){
return ((n>0) && ((n & n-1) == 0) && (n%3 == 1));
}
let test_no = 64
if (isPowerOfFour(test_no)== true ){
console.log(test_no + " is a power of 4" );
}
else {
console.log(test_no + " is not a power of 4" );
}
|
Output
64 is a power of 4
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 9: Alternative approach: Using built-in methods (one-liner)
Any number that is a power of 4 has the following properties:
1. It is a power of 2 (that is, only its leftmost bit is set)
2. It has an even number of unset bits.
Most languages have in-built methods to count the number of leading and trailing unset bits, such as __builtin_clz in C++, numberOfLeadingZeros() in Java, and bit_length() in Python3. If a number is a power of 2, then it shall only have one set bit. Therefore, if the number of trailing zeros is even, then it is a power of 4.
The approach is shown below:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPowerOfFour( int n)
{
return !(n & (n - 1)) && ((32 - __builtin_clz(n)) % 2);
}
int main()
{
int test_no = 64;
if (isPowerOfFour(test_no))
cout << test_no << " is a power of 4\n" ;
else
cout << test_no << " is not a power of 4\n" ;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static boolean isPowerOfFour( int n)
{
return ((n & (n - 1 )) == 0 ) && (( 32 - Integer.numberOfLeadingZeros(n)) % 2 != 0 );
}
public static void main(String[] args)
{
int test_no = 64 ;
if (isPowerOfFour(test_no))
System.out.println(test_no + " is a power of 4\n" );
else
System.out.println(test_no + " is not a power of 4\n" );
}
}
|
Python3
import math
def isPowerOfFour(n):
return ((n & (n - 1 )) = = 0 ) and (( 32 + 1 - (math.floor(math.log(n) / math.log( 2 )))) % 2 = = 1 )
test_no = 64
if (isPowerOfFour(test_no)):
print (test_no, "is a power of 4" )
else :
print (test_no, "is not a power of 4" )
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static bool isPowerOfFour( int n)
{
return ((n & (n - 1)) == 0) && ((32 + 1 - (Math.Floor(Math.Log(n) / Math.Log(2)))) % 2 == 1);
}
public static void Main( string [] args)
{
int test_no = 64;
if (isPowerOfFour(test_no))
Console.WriteLine(test_no + " is a power of 4\n" );
else
Console.WriteLine(test_no + " is not a power of 4\n" );
}
}
|
Javascript
function isPowerOfFour(n)
{
return !(n & (n - 1)) && (32 + 1 - (Math.floor(Math.log(n) / Math.log(2)))) % 2;
}
let test_no = 64;
if (isPowerOfFour(test_no))
console.log(test_no + " is a power of 4" );
else
console.log(test_no + " is not a power of 4" );
|
Output
64 is a power of 4
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 10: Using math module and is_integer
1. This function takes an integer n as input and returns True if it is a power of 4 and False otherwise.
2. The math.log function is used to calculate the logarithm base 4 of n.
3. The is_integer method is used to check if the result is an integer.
C++
#include <iostream>
#include <cmath>
using namespace std;
bool is_power_of_4_log( int n) {
if (n <= 0) {
return false ;
}
return fmod ( log (n) / log (4), 1) == 0;
}
int main() {
cout << is_power_of_4_log(16) << endl;
cout << is_power_of_4_log(36) << endl;
cout << is_power_of_4_log(20) << endl;
return 0;
}
|
Python3
import math
def is_power_of_4_log(n):
if n < = 0 :
return False
return math.log(n, 4 ).is_integer()
print (is_power_of_4_log( 16 ))
print (is_power_of_4_log( 36 ))
print (is_power_of_4_log( 20 ))
|
C#
using System;
class Program
{
static bool IsPowerOf4Log( int n)
{
if (n <= 0) {
return false ;
}
return Math.IEEERemainder(Math.Log(n) / Math.Log(4),
1)
== 0;
}
static void Main( string [] args)
{
Console.WriteLine(IsPowerOf4Log(16));
Console.WriteLine(IsPowerOf4Log(36));
Console.WriteLine(IsPowerOf4Log(20));
}
}
|
Javascript
function is_power_of_4_log(n) {
if (n <= 0) {
return false ;
}
return Number.isInteger(Math.log(n) / Math.log(4));
}
console.log(is_power_of_4_log(16)==1? "True" : "False" );
console.log(is_power_of_4_log(36)==1? "True" : "False" );
console.log(is_power_of_4_log(20)==1? "True" : "False" );
|
Java
public class Main {
public static void main(String[] args) {
System.out.println(isPowerOf4Log( 16 ));
System.out.println(isPowerOf4Log( 36 ));
System.out.println(isPowerOf4Log( 20 ));
}
public static boolean isPowerOf4Log( int n) {
if (n <= 0 ) {
return false ;
}
return (Math.log(n) / Math.log( 4 )) % 1 == 0 ;
}
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Please write comments if you find any of the above codes/algorithms incorrect, or find other ways to solve the same problem
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
31 Mar, 2023
Like Article
Save Article