导航
导航
文章目录
  1. 题目
  2. 翻译

LeetCode-263.Ugly Number

题目

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. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

翻译

编写程序,验证一个给出数字是否为丑数。
丑数是质因子只有2、3、5的正数。例如,6、8是丑的,14不是,因为它有质因子7。
另外1也被当作丑数。

丑数具有如下性质:当一个数排除所有的2、3、5因子后,等于1(注意对0的操作):

1
2
3
4
5
6
7
8
9
10
11
12
public class Solution {
public boolean isUgly(int num) {

while (num % 2 == 0)
num /= 2;
while (num % 3 == 0)
num /= 3;
while (num % 5 == 0)
num /= 5;
return num == 1;
}
}