LeetCode 326

Power of Three

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?

 

 1 /*************************************************************************
 2     > File Name: LeetCode326.c
 3     > Author: Juntaran    
 4     > Mail: Jacinthmail@gmail.com
 5     > Created Time: 2016年05月10日 星期二 02时42分09秒
 6  ************************************************************************/
 7 
 8 /*************************************************************************
 9     
10     Power of Three
11     
12     Given an integer, write a function to determine if it is a power of three.
13 
14     Follow up:
15     Could you do it without using any loop / recursion?
16 
17  ************************************************************************/
18 
19 #include "stdio.h"
20 
21 int isPowerOfThree(int n) {
22     double tmp = log10(n)/log10(3);
23     return tmp == (int)tmp ? 1 : 0;
24 }
25 
26 int main()
27 {
28     int n = 9;
29     int ret = isPowerOfThree(n);
30     printf("%d\n",ret);
31     
32     n = 10;
33     ret = isPowerOfThree(n);
34     printf("%d\n",ret);
35     
36     return 0;
37 }

 

posted @ 2016-05-10 19:15  Juntaran  阅读(139)  评论(0编辑  收藏  举报