263. Ugly Number

Problem

Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

Example

Input: 6
Output: true
Explanation: 6 = 2 × 3
Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2
Input: 14
Output: false 
Explanation: 14 is not ugly since it includes another prime factor 7.

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    bool isUgly(int num) {
        if(num<1)
            return false;
        while(1){
            if(num==1)
                return true;
            else if(num%5==0){
                num=num/5;
                continue;
            }
            else if(num%3==0){
                num=num/3;
                continue;
            }
            else if(num%2==0){
                num=num/2;
                continue;
            }   
            else
                return false;
        }
    }
};

Result

263. Ugly Number_第1张图片
263. Ugly Number.png

你可能感兴趣的:(263. Ugly Number)