326. Power of Three,342. Power of Four

Power of Three

Given an integer, write a function to determine if it is a power of three.

Example 1:

Input: 27
Output: true

Example 2:

Input: 0
Output: false

Example 3:_

Input: 9
Output: true

Example 4:

Input: 45
Output: false

Follow up:
Could you do it without using any loop / recursion?

3 ^ 1 = 3;
3 ^ 2 = 9;
3 ^ 4 = 27;
3 ^ 5 = 81;
3 ^ 6 = 243;
3 ^ 7 = 729;
3 ^ 8 = 2187;
3 ^ 9 = 6561;
...

It’s easy to find all the powers of three is odd, and they can’t be divisible by two or any even number. And all them are be formed by number three, which means all their factors are the powers of three, for example, the factor set of 81 is {1, 81, 3, 27, 9}. In another word, if a number is divisible by another greater number which is some power of three, then it’s the some power of three too.
Then we find the maximum power of three A in the range of [1, INT_MAX], if A % number == 0, then number if the a power of three.

int main()
{
    int start = 3;
    for(int i = 0; i <= 20; i++)
    {
        cout << start << endl;
        start *= 3;
    }
    return 0;
}

This is the output of the above code

3
9
27
81
243
729
2187
6561
19683
59049
177147
531441
1594323
4782969
14348907
43046721
129140163
387420489
1162261467
-808182895
1870418611

Then we know the greatest power of three in the 32 signed number is 1162261467.

bool isPowerOfThree(int n)
{
    if (n == 0)return false;
    return 1162261467 % n == 0;
}

Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example 1:

Input: 16
Output: true

Example 2:

Input: 5
Output: false

Follow up: Could you solve it without loops/recursion?

If variable num is a power of 4, it’s a power of 2 too. We detect whether it’s a power of 2 firstly.
There is only one 1 in any 2’s power’s binary form, look the following examples:

2 -> 10, 1 -> 01, 2 & 1 = 0
4 -> 100, 3 -> 011, 4 & 3 = 0
8 -> 1000, 7 -> 0111, 8 & 7 = 0
16 -> 10000, 15 -> 01111, 15 & 14 = 0
...

If a number n is the some power of 2, then it does AND operation with n - 1 bit-by-bit equals 0.

There are some examples of powers of 4:

4 -> 100
16 -> 10000
64 -> 1000000
256 -> 100000000
1024 -> 10000000000
...

For any power of 4, their binary state’s only 1 is on an odd position from right to left. And it’s obvious that there is no a number whose binary form has only one 1 and the 1 is put on the odd position, which is not a power of 4.
The hexadecimal number 0x55555555, its odd positions in its binary form are all filled with 1, and its even positions in its binary form are all filled with 0. So only those numbers whose binary form have only one 1 and the 1’s position index is odd from right to left do AND operation with 0x55555555 bit-by-bit equals themselves.
Then we get the reasonable solution:

bool isPowerOfFour(int num)
{
    if (num <= 0)return false;
    if (num & (num - 1) != 0)return false;
    return (num & 0x55555555) == num;
}

www.sunshangyu.top

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值