C++: print out all the prime factors and see whether any one is greater than 5


  • 0
    T
    class Solution {
    public:
        bool isUgly(int num) {
            if(num == 1)
                return true;
            int n = num;
            if(n<=0)
                return false; 
            while (n%2 == 0)
                n = n/2;
                
            for(int i=3; i<=sqrt(n); i=i+2)
            {
                while(n%i==0)
                {
                    if(i>5)
                        return false;
                    n = n/i;
                }
            }
            
            if(n>5)
                return false;
                
            return true;
        }
    };

Log in to reply
 

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