Two kind of answers but one works, another not, why??


  • 0
    J
    class Solution {
    public:
        bool isPowerOfThree(int n) {
    
            float log_num = log10(n) / log10(3);
            return log_num == int(log_num);
        }
    };
    
    class Solution {
    public:
        bool isPowerOfThree(int n) {
            return (int(log10(n) / log10(3)) - log10(n) / log10(3) == 0);
        }
    };
    

    Why the second class's function doesnt work??


  • 1
    T

    float is not accurate enough. Use double and you will get the correct result.


  • 0
    J
    This post is deleted!

  • 0
    Q

    class Solution {
    public:
    bool isPowerOfThree(int n) {
    double log_num = (double)log10(n) / log10(3);
    return log_num == int(log_num);
    }
    };
    this is right


Log in to reply
 

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