Competitive ProgrammingEasy

Ugly Numbers

Detailed guide and Python implementation for the 'Ugly Numbers' problem.

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?
Consider using Dynamic Programming-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.