Split N as the sum of K numbers satisfying the given conditions | list to store prime numbers ; Function to generate prime numbers using SieveOfEratosthenes ; Boolean array to store primes ; If p is a prime ; Mark all its multiples as non - prime ; Print all prime numbers ; Function to generate n as the sum of k numbers where atleast K - 1 are distinct and are product of 2 primes ; Stores the product of every pair of prime number ; Sort the products ; If sum exceeds n ; Otherwise , print the k required numbers ; Driver Code
primes = [ ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime = [ True ] * 10005 NEW_LINE p = 2 NEW_LINE while p * p <= 1000 : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , 1001 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , 1001 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT primes . append ( p ) NEW_LINE DEDENT DEDENT DEDENT def generate ( n , k ) : NEW_LINE INDENT prod = [ ] NEW_LINE SieveOfEratosthenes ( ) NEW_LINE l = len ( primes ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT for j in range ( i + 1 , l ) : NEW_LINE INDENT if ( primes [ i ] * primes [ j ] > 0 ) : NEW_LINE INDENT prod . append ( primes [ i ] * primes [ j ] ) NEW_LINE DEDENT DEDENT DEDENT prod . sort ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( k - 1 ) : NEW_LINE INDENT sum += prod [ i ] NEW_LINE DEDENT if ( sum > n ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( k - 1 ) : NEW_LINE INDENT print ( prod [ i ] , end = " , ▁ " ) NEW_LINE DEDENT print ( n - sum ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 52 NEW_LINE k = 5 NEW_LINE generate ( n , k ) NEW_LINE DEDENT