How to check if an integer is a power of 3?

前端 未结 23 1103
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 08:15

I saw this question, and pop up this idea.

23条回答
  •  伪装坚强ぢ
    2020-12-02 08:22

    Here is a nice and fast implementation of Ray Burns' method in C:

    bool is_power_of_3(unsigned x) {
        if (x > 0x0000ffff)
            x *= 0xb0cd1d99;    // multiplicative inverse of 59049
        if (x > 0x000000ff)
            x *= 0xd2b3183b;    // multiplicative inverse of 243
        return x <= 243 && ((x * 0x71c5) & 0x5145) == 0x5145;
    }
    

    It uses the multiplicative inverse trick for to first divide by 3^10 and then by 3^5. Finally, it needs to check whether the result is 1, 3, 9, 27, 81, or 243, which is done by some simple hashing that I found by trial-and-error.

    On my CPU (Intel Sandy Bridge), it is quite fast, but not as fast as the method of starblue that uses the binary logarithm (which is implemented in hardware on that CPU). But on a CPU without such an instruction, or when lookup tables are undesirable, it might be an alternative.

提交回复
热议问题