# 1 line C++ no recursion/loop

• ``````class Solution {
public:
bool isPowerOfThree(int n) {
return fmod(log10(n)/log10(3), 1)==0;
}
};``````

• is this a solution that have no sense at all in the term of programming?

• Can you tell me why you use log10 not log? thanks!

• This post is deleted!

• why it round off error when n=243?

• why i can't write fmod(log10(n),log10(3))?

• The function is (log10(n)/log10(3)) mod 1, not log10(n) mod log10(3).

• @butterwang0
I'm also confused - shouldn't `fmod(log10(n), log10(3)) == 0` if `fmod(log10(n)/log10(3), 1) == 0`?

• Are you sure log10() function doesn't use recursion or loop inside?

• This post is deleted!

• @SuperCC
log10(6) = 0.77815125
log10(3) = 0.477121255
0.77815125 % 0.477121255 = 0.301029995

Given above, shouldn't `fmod(log10(6), log10(3))` = 0.301029995 instead of 0? Can you explain?

• @Vickymay i think that 10 is rational number.

• Here is why log10() was chosen,

``````cout << (fmod(log(81)/log(3), 1)==0);       // 1
cout << (fmod(log10(81)/log10(3), 1)==0);   // 1

cout << (fmod(log(243)/log(3), 1)==0);      // 0
cout << (fmod(log10(243)/log10(3), 1)==0);  // 1

cout << (fmod(log(729)/log(3), 1)==0);      // 1
cout << (fmod(log10(729)/log10(3), 1)==0);  // 1
``````

You'll be happily kicked out if you compare double with 0 in interview.

---===---

Just found the precision bug is also explained in this thread

https://discuss.leetcode.com/topic/33536/a-summary-of-all-solutions-new-method-included-at-15-30pm-jan-8th

• I think this is similar to the "Power Of Four", that you should not use loop/recursion/library functions.

• There is a logarithm algorithm :

logb(n) = loge(n) / loge(b), e can be changed to any base.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.