Ugly Number - Problem
An ugly number is a positive integer which does not have a prime factor other than 2, 3, and 5.
Given an integer n, return true if n is an ugly number, otherwise return false.
Note: 1 is typically treated as an ugly number. Negative numbers and zero are not ugly numbers.
Input & Output
Example 1 — Basic Ugly Number
$
Input:
n = 6
›
Output:
true
💡 Note:
6 = 2 × 3, so it only has prime factors 2 and 3, making it ugly
Example 2 — Not Ugly Number
$
Input:
n = 14
›
Output:
false
💡 Note:
14 = 2 × 7, and 7 is a prime factor other than 2, 3, 5, so 14 is not ugly
Example 3 — Edge Case
$
Input:
n = 1
›
Output:
true
💡 Note:
1 has no prime factors, so by convention it is considered an ugly number
Constraints
- -231 ≤ n ≤ 231 - 1
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given integer n
2
Process
Divide out all 2s, 3s, and 5s
3
Output
True if result is 1, false otherwise
Key Takeaway
🎯 Key Insight: Keep dividing by 2, 3, 5 until you can't anymore - if you reach 1, the number is ugly
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code