Construct longest possible sequence of unique elements with given LCM | Python3 program to implement the above approach ; Function to construct an array of unique elements whose LCM is N ; Stores array elements whose LCM is N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If N is divisible by i ; Insert i into newArr [ ] ; If N is not perfect square ; Sort the array newArr [ ] ; Print array elements ; Driver Code ; Given N ; Function Call
from math import sqrt , ceil , floor NEW_LINE def constructArrayWithGivenLCM ( N ) : NEW_LINE INDENT newArr = [ ] NEW_LINE for i in range ( 1 , ceil ( sqrt ( N + 1 ) ) ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT newArr . append ( i ) NEW_LINE if ( N // i != i ) : NEW_LINE INDENT newArr . append ( N // i ) NEW_LINE DEDENT DEDENT DEDENT newArr = sorted ( newArr ) NEW_LINE for i in newArr : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE constructArrayWithGivenLCM ( N ) NEW_LINE DEDENT