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.具体代码如下:
public class Solution {
public boolean isUgly(int num) {
for(int i=2;i<6&&num>0;i++){
while(num%i==0){
num=num/i;
}
}
return (num==1);
}
}