freephys
2/6/2020 - 10:13 PM

gcd_lcm_between_two_set

https://www.hackerrank.com/challenges/between-two-sets/problem

2 4 16 32 96 Sample Output

3 Explanation

2 and 4 divide evenly into 4, 8, 12 and 16. 4, 8 and 16 divide evenly into 16, 32, 96.

4, 8 and 16 are the only three numbers for which each element of a is a factor and each is a factor of all elements of b.

#
# Complete the 'getTotalX' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER_ARRAY a
#  2. INTEGER_ARRAY b
#

def getTotalX(a, b):
    # Write your code here
    
    gcd = b[0]
    for i in range(1, len(b)):
        gcd = math.gcd(gcd, b[i])
    lcm = a[0]
    for i in range(1, len(a)):
        lcm = (lcm*a[i])//(math.gcd(lcm, a[i]))
    if gcd %lcm ==0:
        return len([i*lcm for i in range(1, gcd//lcm+1) 
                          if gcd%(i*lcm)==0])
    return 0