Back to Practice Dashboard
Competitive ProgrammingEasy

Ugly Numbers

Learn how to solve the 'Ugly Numbers' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function nth_ugly_number(n) that returns the nth ugly number. Ugly numbers are positive numbers whose only prime factors are 2, 3, or 5. By convention, 1 is treated as an ugly number.

Constraints
  • 1 <= n <= 500

Examples

Example 1
Input
nth_ugly_number(10)
Output
12
Explanation

The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12.

Example 2
Input
nth_ugly_number(15)
Output
24
Explanation

The first 15 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24.

Need a Hint?
Define subproblem states, establish the recurrence relation, and use memoization (top-down) or tabulation (bottom-up).
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor