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

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

I saw this question, and pop up this idea.

23条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 08:38

    Recursively divide by 3, check that the remainder is zero and re-apply to the quotient.

    Note that 1 is a valid answer as 3 to the zero power is 1 is an edge case to beware.

提交回复
热议问题