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,5相除就會最終為0
public boolean isUgly(int num) { // Write your code here if (num <= 0) { return false; } while (num % 2 == 0) { num /= 2; } while (num % 3 == 0) { num /= 3; } while (num % 5 == 0) { num /= 5; } if (num == 1) { return true; } return false; }
新聞熱點
疑難解答