263. Ugly Number
题目描述:
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.
题目翻译
给一个数字,写一个程序来判断它是否是一个丑陋的数字。
丑陋的数字是一个只包含质因子2,3,5的正整数。例如,6,8是丑陋的数字而14不丑陋因为它包含另一个质因子7.
特别的:1也被看做一个丑陋的数字。
解题方案
标签: Math
思路: 判断给出的数字能否被2,3,5整除(余数为1),直至结果为1为止,此时是丑陋的数字。否则不是丑陋的数字。
代码:
class Solution {
public boolean isUgly(int num) {
if(num<1) return false;
while(num%2==0) num=num/2;
while(num%3==0) num=num/3;
while(num%5==0) num=num/5;
if(num==1) return true;
else return false;
}
}