判断一个整数是否为丑数

丑数就是只包含质因数 2, 3, 5 的正整数,一般的认为1是第一个丑数。

例如2*3=6是丑数,2*2*2=8也是丑数。

package simple;

public class Ugly {
	public static void main(String[] args) {
		boolean ugly = isUgly(7);
		System.out.println(ugly);
	}

	public static boolean isUgly(int num) {
		if (num < 1) {
			return false;
		} else {
			while (num % 2 == 0) {
				num /= 2;
			}
			while (num % 3 == 0) {
				num /= 3;
			}
			while (num % 5 == 0) {
				num /= 5;
			}
			if (num == 1) {
				return true;
			} else {
				return false;
			}
		}
	}
}

 

你可能感兴趣的:(面试题)