# 【LeetCode】 263. Ugly Number ## Description > 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`. > Note: > * `1` is typically treated as an ugly number. > * Input is within the 32-bit signed integer range: `[−2^31, 2^31 − 1]`. > 寫一個程式去確認給予的數字是否為醜數。 > 醜數是指一個正整數它的質因數只有包含`2`、`3`、`5`。 > 注意: > * `1`是一個醜數。 > * 輸入數字是32位元的有號整數,其範圍為`[−2^31, 2^31 − 1]`。 ## Example: ``` Example 1: Input: 6 Output: true Explanation: 6 = 2 × 3 Example 2: Input: 8 Output: true Explanation: 8 = 2 × 2 × 2 Example 3: Input: 14 Output: false Explanation: 14 is not ugly since it includes another prime factor 7. ``` ## Solution * 用`%(modulo)`去判斷是否可以整除。 * 用`while`去不停地除。 * 記得判斷是否為整數。 ### Code ```C++=1 class Solution { public: bool isUgly(int num) { if(num <= 0) return false; while(!(num % 2)) num /= 2; while(!(num % 3)) num /= 3; while(!(num % 5)) num /= 5; return num == 1; } }; ``` ###### tags: `LeetCode` `C++`