定义方法,判断一个数是否是丑数。丑数就是只包含质因数 2, 3, 5 的正整数。
比如:6是一个丑数6 = 2 × 3
8是一个丑数8 = 2 × 2 × 2
14不是丑数,因为它包含了另外一个质因数 7
public class UglyNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (PrimeFactor(n)) {
System.out.println(n + "是丑数!");
} else {
System.out.println(n + "不是丑数!");
}
}
public static boolean PrimeFactor(int n) {
//如果n能被2整除,就一直除下去,直到等于1为止
while (n % 2 == 0) {
n = n / 2;
}
while (n % 3 == 0) {
n = n / 3;
}
while (n % 5 == 0) {
n = n / 5;
}
return true;
}
}