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.
用的比较笨的办法,思路比较简单。算法如下:
public class Solution {
public boolean isUgly(int num) {
long result = 0L;
if(num==1){
return true;
}
for(int i=0; i<32; i++){
for(int j=0; j<21; j++){
for(int k=0; k<14; k++){
result = (long) (Math.pow(2,i)*Math.pow(3,j)*Math.pow(5,k));
if(result == num){
return true;
}
}
}
}
return false;
}
}