Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | Python3 program for the above approach ; Function to print the required N - th subset ; Nearest power of 2 <= N ; Now insert k ^ p in the answer ; Update N ; Print the subset ; Driver Code
import math NEW_LINE def printSubset ( N , K ) : NEW_LINE INDENT answer = " " NEW_LINE while ( N > 0 ) : NEW_LINE INDENT p = int ( math . log ( N , 2 ) ) NEW_LINE answer = str ( K ** p ) + " ▁ " + answer NEW_LINE N = N % ( 2 ** p ) NEW_LINE DEDENT print ( answer ) NEW_LINE DEDENT N = 5 NEW_LINE K = 4 NEW_LINE printSubset ( N , K ) NEW_LINE