Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

Write a Python program to find the nth super ugly number from a given prime list of size k using Heap queue algorithm.

 Write a Python program to find the nth super ugly number from a given prime list of size k using Heap queue algorithm.

INPUT:

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]

n = 12

primes = [2,7,13,19]

print(nth_Super_Ugly_Number(n, primes))


OUTPUT:




Post a Comment

0 Comments