Ugly Numbers

/**
 * Created by chris on 2015/12/17.
 *
 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 UglyNumber {
    public boolean isUglyNumber(int num){

        int ugly[] = {2,3,5};
        if (1 == num) return true;
        if (0 == num) return false;

        for (int i = ugly.length-1; i >= 0  ; i--) {
            while (0 == num % ugly[i]){
                num = num / ugly[i];
            }
            if (1 == num) return true;

        }
        return false;
    }
}
Tagged on: ,

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>