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以外的其他素數整除的數。1,2,3,4,5,6,8,9,10,12,15是最前面的11個丑數。
編寫程序判斷一個給定的數字是否為“丑陋數” ugly number
丑陋數是指只包含質因子2, 3, 5的正整數。例如,6, 8是丑陋數而14不是,因為它包含額外的質因子7
注意,數字1也被視為丑陋數
讓其重復的除2,3,5
1 class Solution { 2 public: 3 bool isUgly(int num) { 4 if(num<=0) return false; 5 vector<int> v{2,3,5}; 6 7 for(auto c:v) 8 { 9 while(num%c==0) num/=c; 10 } 11 return num==1; 12 } 13 };