An ugly number is a positive integer whose prime factors are limited to 2, 3 and 5. By convention, 1 is the first ugly number, so the sequence starts 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, and so on.
Given an integer n, return the nth ugly number.
Function Signature
def nth_ugly_number(n: int) -> int:
Rules
-
n
is 1-based:
n = 1
returns 1, the first ugly number.
-
A number with any prime factor other than 2, 3 and 5 is not ugly. For example, 7 and 14 (
2 * 7
) are not in the sequence.
Constraints
Examples
Input: n = 10
Output: 12
The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, so the 10th is 12. The number 7 is skipped because it is a prime other than 2, 3 and 5, and 11 is skipped for the same reason.
Input: n = 1
Output: 1
1 has no prime factors at all, and by convention it counts as the first ugly number.
Input: n = 15
Output: 24
After 12 the sequence continues 15, 16, 18, 20, 24. The numbers 13, 14, 17, 19, 21, 22 and 23 each have a prime factor outside 2, 3 and 5, so the 15th ugly number is 24.