---
title: "Power of Three"
author: "KickCode"
date: "2016-02-28T04:58:57Z"
tags: []
categories: ["编程语言"]
slug: "power-of-three"
draft: false
lastmod: "2016-02-28T04:58:57Z"
id: 2279307
url: "https://kickcode.iteye.com/blog/2279307"
---

Given an integer, write a function to determine if it is a power of three.

Follow up:
Could you do it without using any loop / recursion?

判断一个整数是否为3的幂。如果一个整数是3的幂，那么就可以表示为
3 ^ x = n  <span style="color:violet">=></span> log(3^x) = log(n) <span style="color:violet">=></span> xlog(3) = log(n) <span style="color:violet">=></span> x = log(n) / log(3)。我们只需要判断x是否为整数就可以了，代码如下：

```java

public class Solution {
    public boolean isPowerOfThree(int n) {
        if(n <= 0) return false;
        double d = Math.log10(n) / Math.log10(3);
        return d % 1 == 0 ? true : false;
    }
}

```