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.
列如:1,2,3,4,5,6,8,9,10,12....都是丑数,代码如下:
public class Solution {
public boolean isUgly(int num) {
if(num==1){
return true;
}
if(num==0){
return false;
}
boolean flag=false;
while(num!=0){
if(num%2==0){
flag=true;
if(num==2){
return true;
}
num/=2;
}
if(num%3==0){
flag=true;
if(num==3){
return true;
}
num/=3;
}
if(num%5==0){
flag=true;
if(num==5){
return true;
}
num/=5;
}
if(flag){
flag=false;
}else{
return false;
}
}
return true;
}
}