Skip to main content

0263 - Ugly Number

https://leetcode.com/problems/ugly-number/

Problem Statement

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.

Example 1:

Input: n = 6
Output: true
Explanation: 6 = 2 × 3

Example 2:

Input: n = 1
Output: true
Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.

Example 3:

Input: n = 14
Output: false
Explanation: 14 is not ugly since it includes the prime factor 7.

Constraints:

  • 231n2311-2^{31} \leq n \leq 2^{31} - 1

Approach 1: Brute Force

Remove all power of 22 from nn by dividing it by 22 until nn is no longer divisible by 22. Repeat the same for 33 and 55. If nn becomes 11 then it doesn't have any prime factor other than these three, thus it is an ugly number. Otherwise nn is not an ugly number.

Written by @Ishwarendra
class Solution {
public:
bool isUgly(int n) {
// Since ugly numbers are positive
if (n <= 0) return false;

array<int, 3> nums {2, 3, 5};

for (int num : nums) {
while (n % num == 0) n /= num;
}

return n == 1;
}
};
  • Time Complexity: O(log(n))O(log(n)).
  • Space Complexity: O(1)O(1).