Write a program to check whether a given number was an ugly number.
Ugly numbers is positive numbers whose prime factors only include 2, 3, 5 . For example, was ugly while was not 6, 8 14 ugly since it includes another prime factor 7 .
Note that's 1 typically treated as an ugly number.
This is the new question which is added today. There's nothing to say. As long as the definition of ugly number is clear, you can write it well.
Because the prime factor can only be 2,3,5. So as long as a number cannot be decomposed into a multiplicative that contains only these three numbers then it is not a ugly number.
The code is as follows. ~
public class Solution {public boolean isugly (int num) { if (num<=0) { return false; } if (num==1) { return true; } while (num!=1) { if (num%2==0) { num=num/2; } else if (num%3==0) { num=num/3; } else if (num%5==0) { num=num/5; } else{ return false; } } return true; }}
[Leetcode] Ugly number (A New Question Added today)