[LeetCode] Ugly Number

263. Ugly Number

An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.

Given an integer n, return true if n is an ugly number.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
bool isUgly(int n) {
if(n == 0) return 0;
while(n % 2 == 0) n /= 2;
while(n % 3 == 0) n /= 3;
while(n % 5 == 0) n /= 5;
return n == 1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/18/PS/LeetCode/ugly-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.