Ctrl K

Nth Number Built From Given Primes

Medium
You are given a list of primes factors. Call a number *built* if it is positive and every one of its prime factors appears in factors. The number 1 also counts as built, since it has no prime factors. List all built numbers in increasing order and return the n-th one (counting from 1). The answer is guaranteed to fit in a signed 32-bit integer.
Example 1
n=8, factors=[3, 5] → 45

The qualifying numbers in order are 1, 3, 5, 9, 15, 25, 27, 45. The eighth one is 45.

Example 2
n=6, factors=[2, 11] → 16

The sequence goes 1, 2, 4, 8, 11, 16, so the sixth number is 16.

Example 3
n=1, factors=[7, 13] → 1

1 has no prime factors at all, so it always comes first.

Constraints
  • 1 ≤ n ≤ 10⁵
  • 1 ≤ len(factors) ≤ 100
  • 2 ≤ factors[i] ≤ 1000
  • All factors[i] are prime, distinct and sorted in increasing order
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsChain of Multiples MediumBest Rotation for a Weighted Sum MediumDuel for the End Cards MediumBiggest Division Chain MediumFour-Corner Rectangles Medium
loading Python…
Input
n=8, factors=[3, 5]
Expected
45

Run checks the first 3 tests in your browser. Submit checks all 97 tests on the server, hidden ones included.