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
Ugly Number Check: Process OverviewInputn = 6Division Process6 ÷ 2 = 33 ÷ 3 = 11 ÷ 5 = 1 (no change)OutputtrueExamples:✓ 6 = 2×3 → ugly✓ 1 → ugly✗ 14 = 2×7 → not ugly✓ 30 = 2×3×5 → ugly✗ 0, -5 → not ugly✓ 25 = 5² → ugly
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
Asked in
Facebook 12 Google 8 Amazon 6
89.0K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen