How do you check if a number is a power of 4 in python?

Given a positive number, check if it is a power of four or not.

Practice this problem

Approach 1

A simple solution is to calculate log4n for a given number n. If it returns an integral value, then we can say that the number is a power of four.

This approach is demonstrated below in C++, Java, and Python:

C++


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

#include

#include

usingnamespacestd;

// Returns true if `n` is a power of four

boolcheckPowerOf4(unsignedn)

{

    // find `log4(n)`

    doublei=log(n)/ log(4);

    // return true if `log4(n)` is an integer

    return i==trunc(i);

}

int main()

{

    unsignedn= 256;

    if(checkPowerOf4(n)){

        cout<<n<<" is a power of 4";

    }

    else{

        cout <<n<<" is not a power of 4";

    }

    return0;

}

Download  Run Code

Output:

256 is a power of 4

Java


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

classMain

{

    // Returns true if `n` is a power of four

    public staticbooleancheckPowerOf4(intn)

    {

        // find `log4(n)`

        doublei= Math.log(n)/ Math.log(4);

        // return true if `log4(n)` is an integer

        returni==Math.floor(i);

    }

    publicstaticvoid main(String[]args)

    {

        intn=256;

        if(checkPowerOf4(n)){

            System.out.println(n+" is a power of 4");

        }

        else{

            System.out.println(n+" is not a power of 4");

        }

    }

}

Download  Run Code

Output:

256 is a power of 4

Python


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

frommathimportlog,floor

# Returns true if `n` is a power of four

defcheckPowerOf4(n):

    # find `log4(n)`

    i =log(n)/log(4)

    # return true if `log4(n)` is an integer

    returni== floor(i)

if__name__=='__main__':

    n=256

    if checkPowerOf4(n):

        print(n, 'is a power of 4')

    else:

        print(n,'is not a power of 4')

Download  Run Code

Output:

256 is a power of 4

Approach 2

The given number n is a power of 4 if it is a power of 2 and its only set bit is present at even position (0, 2, 4, …).

How to check for power of 2?

The expression n & (n-1) will unset the rightmost set bit of a number. If the number is a power of 2, it has only a 1–bit set, and n & (n-1) will unset the only set bit. So, we can say that n & (n-1) returns 0 if n is a power of 2; otherwise, it’s not a power of 2.

We can also the expression (n & -n) == n to check if a positive integer is a power of 2 or not. For more details, refer to this post.

How to check position of the set bit?

To check the position of its set bit, we can use 0xAAAAAAAA as a mask. The mask 0xAAAAAAAA has 1 in all its odd position. So if the expression !(n & 0xAAAAAAAA) is true, the position of the set bit in n is even.

(0xAAAAAAAA)16 = (10101010101010101010101010101010)2

Following is the C++, Java, and Python program that demonstrates it:

C++


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include

usingnamespacestd;

// Returns true if `n` is a power of four

bool checkPowerOf4(unsignedn)

{

    // return true if `n` is a power of 2, and its only

    // set bit is present at even position

    returnn&&!(n &(n-1))&& !(n&0xAAAAAAAA);

}

int main()

{

    unsignedn= 256;

    if(checkPowerOf4(n)){

        cout<<n<<" is a power of 4";

    }

    else{

        cout <<n<<" is not a power of 4";

    }

    return0;

}

Download  Run Code

Output:

256 is a power of 4

Java


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

classMain

{

    // Returns true if `n` is a power of four

    public staticbooleancheckPowerOf4(intn)

    {

        // return true if `n` is a power of 2, and its only

        // set bit is present at even position

        return n!=0&&(n& (n-1))==0 &&(n&0xAAAAAAAA)== 0;

    }

    publicstaticvoid main(String[]args)

    {

        intn=256;

        if(checkPowerOf4(n)){

            System.out.println(n+" is a power of 4");

        }

        else{

            System.out.println(n+" is not a power of 4");

        }

    }

}

Download  Run Code

Output:

256 is a power of 4

Python


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

# Returns true if `n` is a power of four

defcheckPowerOf4(n):

    # return true if `n` is a power of 2, and its only

    # set bit is present at even position

    returnnandnot (n&(n- 1))andnot(n& 0xAAAAAAAA)

if__name__=='__main__':

    n=256

    if checkPowerOf4(n):

        print(n, 'is a power of 4')

    else:

        print(n,'is not a power of 4')

Download  Run Code

Output:

256 is a power of 4

Approach 3

The given number n is a power of 4 if it is a power of 2 and its remainder is 1 when it is divided by 3. This approach is demonstrated below in C++, Java, and Python:

C++


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include

usingnamespacestd;

// Returns true if `n` is a power of four

bool checkPowerOf4(unsignedn)

{

    // return true if `n` is a power of 2, and

    // the remainder is 1 when divided by 3

    return!(n& (n-1))&&(n%3==1);

}

int main()

{

    unsignedn= 256;

    if(checkPowerOf4(n)){

        cout<<n<<" is a power of 4";

    }

    else{

        cout <<n<<" is not a power of 4";

    }

    return0;

}

Download  Run Code

Output:

256 is a power of 4

Java


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

classMain

{

    // Returns true if `n` is a power of four

    public staticbooleancheckPowerOf4(intn)

    {

        // return true if `n` is a power of 2, and

        // the remainder is 1 when divided by 3

        return ((n&(n- 1))==0)&&(n%3==1);

    }

    publicstaticvoid main(String[]args)

    {

        intn=256;

        if(checkPowerOf4(n)){

            System.out.println(n+" is a power of 4");

        }

        else{

            System.out.println(n+" is not a power of 4");

        }

    }

}

Download  Run Code

Output:

256 is a power of 4

Python


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

# Returns true if `n` is a power of four

defcheckPowerOf4(n):

    # return true if `n` is a power of 2, and

    # the remainder is 1 when divided by 3

    return((n& (n-1))== 0)and(n%3 ==1)

if__name__=='__main__':

    n=256

    if checkPowerOf4(n):

        print(n, 'is a power of 4')

    else:

        print(n,'is not a power of 4')

Download  Run Code

Output:

256 is a power of 4

Exercise: Check if the number is a power of 8 or 16 or not

(Hint – Check the bit pattern. Use mask 0xB6DB6DB6 to check for power of 8 and 0xEEEEEEEE for power of 16)

How do you check whether a number is a power of 4 in Python?

int n = 256;.
if (checkPowerOf4(n)) { System. out. println(n + " is a power of 4");.
} else { System. out. println(n + " is not a power of 4");.

How do you check if n is a power of 2 in Python?

Another solution is to keep dividing the number by two, i.e, do n = n/2 iteratively. In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2.

How do you check if a number is a power of 3 python?

Practical Data Science using Python.
We will use the Logarithm to solve this..
if [log10(n) / log10(3)] mod 1 == 0, then it will be power of three, otherwise not..

How do you check if a number is a power of 10?

A power of 10 is as many number 10s as indicated by the exponent multiplied together. Thus, shown in long form, a power of 10 is the number 1 followed by n zeros, where n is the exponent and is greater than 0; for example, 106 is written 1,000,000.