Find numbers which are multiples of first array and factors of second array | Python3 implementation of the approach ; Function to return the LCM of two numbers ; Function to print the required numbers ; To store the lcm of array a [ ] elements and the gcd of array b [ ] elements ; Finding LCM of first array ; Finding GCD of second array ; No such element exists ; All the multiples of lcmA which are less than or equal to gcdB and evenly divide gcdB will satisfy the conditions ; Driver code
from math import gcd NEW_LINE def lcm ( x , y ) : NEW_LINE INDENT temp = ( x * y ) // gcd ( x , y ) ; NEW_LINE return temp ; NEW_LINE DEDENT def findNumbers ( a , n , b , m ) : NEW_LINE INDENT lcmA = 1 ; __gcdB = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT lcmA = lcm ( lcmA , a [ i ] ) ; NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT __gcdB = gcd ( __gcdB , b [ i ] ) ; NEW_LINE DEDENT if ( __gcdB % lcmA != 0 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT num = lcmA ; NEW_LINE while ( num <= __gcdB ) : NEW_LINE INDENT if ( __gcdB % num == 0 ) : NEW_LINE INDENT print ( num , end = " ▁ " ) ; NEW_LINE DEDENT num += lcmA ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 2 , 4 ] ; NEW_LINE b = [ 16 , 32 , 64 ] ; NEW_LINE n = len ( a ) ; NEW_LINE m = len ( b ) ; NEW_LINE findNumbers ( a , n , b , m ) ; NEW_LINE DEDENT