263. uugly Number (C ++), 263 uglynumber
263. Ugly Number
Write a program to check whether a given number is an ugly number.
Uugly numbers are positive numbers whose prime factors only include2, 3, 5
. For example,6, 8
Are uugly while14
Is not uugly since it has des another prime factor7
.
Note that1
Is typically treated as an uugly number.
The so-called ugly number is the number that cannot be divisible by other prime numbers other than 2, 3, and 5. 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15 are the first 11 ugly figures.
Question:
Write a program to determine whether a given number is an "ugly number" uugly number
An ugly number is a positive integer that contains only the prime factor 2, 3, and 5. For example, 6 and 8 are ugly numbers, but 14 are not because they contain additional quality factor 7.
Note that number 1 is also regarded as an ugly number.
Question completion method:
Division of 2, 3, and 5
C ++ code:
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 };