Find the nth positive integer whose only prime factors are 2, 3 and 5

Quick Overview

Asks for the nth positive integer whose only prime factors are 2, 3 and 5, counting 1 as the first. It tests dynamic programming over a sorted sequence, handling values that can be built in more than one way, and linear-time reasoning.

Find the nth positive integer whose only prime factors are 2, 3 and 5

Company: SambaNova

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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 `n`th ugly number. ### Function Signature ```python 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 - `1 <= n <= 1690` ### Examples ```text 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. ```text Input: n = 1 Output: 1 ``` 1 has no prime factors at all, and by convention it counts as the first ugly number. ```text 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.

Overview: Asks for the nth positive integer whose only prime factors are 2, 3 and 5, counting 1 as the first. It tests dynamic programming over a sorted sequence, handling values that can be built in more than one way, and linear-time reasoning.

SambaNova logo
SambaNova
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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

  • 1 <= n <= 1690

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.