Find number from its divisors | Python3 implementation of the approach Function that returns X ; Function that returns X ; Sort the given array ; Get the possible X ; Container to store divisors ; Find the divisors of x ; Check if divisor ; sort the vec because a is sorted and we have to compare all the elements ; if size of both vectors is not same then we are sure that both vectors can 't be equal ; Check if a and vec have same elements in them ; Driver code ; Function call
import math NEW_LINE def findX ( list , int ) : NEW_LINE INDENT list . sort ( ) NEW_LINE x = list [ 0 ] * list [ int - 1 ] NEW_LINE vec = [ ] NEW_LINE i = 2 NEW_LINE while ( i * i <= x ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT vec . append ( i ) NEW_LINE if ( ( x // i ) != i ) : NEW_LINE INDENT vec . append ( x // i ) NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT vec . sort ( ) NEW_LINE if ( len ( vec ) != int ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT j = 0 NEW_LINE for it in range ( int ) : NEW_LINE INDENT if ( a [ j ] != vec [ it ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT return x NEW_LINE DEDENT a = [ 2 , 5 , 4 , 10 ] NEW_LINE n = len ( a ) NEW_LINE print ( findX ( a , n ) ) NEW_LINE