-2

Hi I have updated this with my code:

Input ->

a=[16,21, 56, 40]
def find_primes(a):
    num_factors=[] 
    for num in a:
       # print('1',num)
        list_of_factors=[]
        i=2
        while num>1:
            #print('2',num)
            if num%i==0:
                list_of_factors.append(i)
               # print('3',list_of_factors)
                num=num/i
               # print('4',num)
                i=i-1
                
            i+=1 
        num_factors.append(list_of_factors)  
    d = {}
    a_list = []
    for i in num_factors:
        for j in i:
            d[j] = d.get(j, 0) + 1
            dictionary_copy = d.copy()
    a_list.append(dictionary_copy)  
    print(a_list)
    return num_factors
    
find_primes(a)

This is the output I am getting:

[{2: 10, 3: 1, 7: 2, 5: 1}]
[[2, 2, 2, 2], [3, 7], [2, 2, 2, 7], [2, 2, 2, 5]]

Here is my question:

Understand since it is a dictionary, the value for the key accumulates.

I want to have the count of unique integers from each list. For eg. [{2:4},{3:1,7:1},{2:3,7:1},{2:3,5:1}] instead of what is given in the output from the code above.

After which, I want to get the max occurrence for each of the integers to calculate the LCM. 2^4 * 3 * 7 * 5

Kindly advise on how we could improve on the code. Appreciate the help.

Ann
  • 1
  • 2
  • 1
    You could factorise them all into primes and then take the product of the smallest set of primes that covers them all. Or you could just multiply the largest number by increasing ints until you get a result that's a multiple of the other numbers. – khelwood Sep 27 '20 at 10:13
  • thank you. this will help me get started @khelwood – Ann Sep 29 '20 at 12:51

1 Answers1

0

This could be one of the approach.

numbers=list(map(int,input().split()))
numbers.sort()
maximum=numbers[0]  #gcd of the input numbers cannot be greater than minimum number in the list.Therefore we are retrieving the mininum number of the list.
    gcd=1               #Initial value of gcd.
    for i in range(1,(maximum+1)):
        flag=0
        for j in range(len(numbers)):
            num=numbers[j]
            if num % i ==0:      #check if the every number in the input is divisible by the value of i
                 flag=1          #if divisible set flag=1 and then check the other numbers of the input.
            else:
                 flag=0          #if any of the number of the input is not divisible then flag =0 and go to next iteration of i.
                 break
        if flag==1:
            gcd=i         #if flag=1 that means every number of the input is divisible by the value of j.Update the value of gcd.
    
    print(gcd)

That can be done in the following way:

for i in num_factors:
    d={}
    for j in i:
       
            try:
             d[j]=d[j]+1
            except KeyError:
             d[j]=1

    dictionary_copy = d.copy()
    a_list.append(dictionary_copy)  
print(a_list)
return num_factors
Aditya_U
  • 61
  • 5
  • @Aditya_U thanks. this was a part of a question I had to solve. this will help me get started. thank you. – Ann Sep 29 '20 at 12:54