human
Generate longest possible array with product K such that each array element is divisible by its previous adjacent element | Function to construct longest array with product K such that each element is divisible by its previous element.
Stores the prime factors of K.
Stores the power to which primefactor i is raised.
Sort prime factors in descending order.
Stores the final array.
Multiply the last element by K.
Print the constructed array.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to generate longest possible array with product K such that each array element is divisible by its previous adjacent element and the other steps required.
 def findLongestArray ( K ) :
	primefactors = [ ]
	K_temp = K
	i = 2
	while i * i <= K :
		count = 0
		while ( K_temp % i == 0 ) :
			K_temp //= i
			count += 1
		if ( count > 0 ) :
			primefactors . append ( [ count , i ] )
		i += 1
	if ( K_temp != 1 ) :
		primefactors . append ( [ 1 , K_temp ] )
	primefactors . sort ( )
	answer = [ primefactors [ 0 ] [ 0 ] , primefactors [ 0 ] [ 1 ] ]
	answer [ - 1 ] *= K
	for i in range ( primefactors [ 0 ] [ 0 ] ) :
		answer [ - 1 ] //= primefactors [ 0 ] [ 1 ]
	print ( " { " , end = " " )
	for i in range ( len ( answer ) ) :
		if ( i == len ( answer ) - 1 ) :
			print ( answer [ i ] , end = " } " )
		else :
			print ( answer [ i ] , end = " , ▁ " )
if __name__ == " _ _ main _ _ " :
	K = 4
	findLongestArray ( K )