0

How should I store a value in list_=[ ] while calculating the LCM between two numbers? I need to store every factor for both the numbers.

My code is:

def lcm(x, y):
    if x > y:
        greater = x
    else:
        greater = y
    lcms =[]
    while(True):

        if((greater % x == 0) and (greater % y == 0)):
            lcm = greater
            lcms.append(lcm)
            break   
        greater += 1
    print(lcms)
    return lcm


t= int(input())
for T in range(1, t+1):
    l= list(map(int, input().split()))
    print (lcm(x=l[0],y=l[1]))

Suppose Test case inputs are t=1 and l = [9, 20], where the output is 180. So, I need to print a list where it contains every LCM integer form both the inputs and then I need to print the LCM between them.

I get the LCM value but don't able to print list = [ ] that contains LCM like this [2,3,5] instead of [2,2,3,3,5] (Reason: I also need to remove the repeating integer values from the list.)

  • 1
    Are you looking at LCM or GCF? – Umang Gupta Feb 27 '18 at 01:59
  • 1
    I don't know if we're going to be able to help you, because there's only one LCM (least common multiple) of a pair of numbers, and so it's not clear what you want to list. Maybe there is some relationship is between what you want and the LCM, but what that relationship is is not obvious. Try to explain it better, and maybe we can help. – Blckknght Feb 27 '18 at 02:33
  • I just want to calculate LCM between 9 and 20 and want to store every factor in a list. – ashwanidv100 Feb 27 '18 at 02:56

1 Answers1

0

No problem I got the solution. Thank you for your help!!

   def lcm(x, y):
        a= x*y
        b=[]
        for i in range(2,a-1):
            if i > 1:
                for j in range(2,i):
                    if (i % j) == 0:
                        break
                else:
                    b.append(i)
        c= len(b)
        d=[]
        for k in b:
            if(a%k==0):
                d.append(k)
        total = 1 
        for x in d:
                total *= x      
        #print(total)

        #print(d)       
        return total


    t= int(input())
    for T in range(1, t+1):
        l= list(map(int, input().split()))
        print (lcm(x=l[0],y=l[1]))