blob: dc0ceec604970b0cfdf8e3c3d4f22edc04118738 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
|
import heapq
def nth_super_ugly_number(n, primes):
uglies = [1]
def gen(prime):
for ugly in uglies:
yield ugly * prime
merged = heapq.merge(*map(gen, primes))
while len(uglies) < n:
ugly = next(merged)
if ugly != uglies[-1]:
uglies.append(ugly)
return uglies[-1]
|