+ 2

Hello everyone .

n=int(input()) for i in range(1,n+1): if n%i==0: print(i) m=int(input()) for i in range(1,m+1): if m%i==0: print(i) As you can see i get the factors of two numbers okay but i want to compare there factor if one factor is matched then it's not co-prime accept 1 else it's co-prime. Can anybody how i will do that .

4th Apr 2022, 3:58 PM
Akash Gupta
Akash Gupta - avatar
6 Réponses
+ 5
Akash Gupta , can you please give us samples with 2 input numbers for each? thanks!
4th Apr 2022, 5:15 PM
Lothar
Lothar - avatar
+ 5
Akash Gupta , (following your approach) as Seb TheS already mentioned, we need to store the factors in collections, but using set is preferred instead of list. we need one set for the first number and an other one for the second number. to avoid getting the factor 1 in the sets, we can start the both ranges (that generates the divisors) with value 2 instead of value 1. in the next step we have to check if there are common factors in the both sets. to perform this we can use intersection() method or "&" operator if the resulting set from the above operation is empty, the input numbers are co-prime
4th Apr 2022, 6:22 PM
Lothar
Lothar - avatar
+ 1
Maybe instead of printing you could append the factors in 2 lists, then you could easily compare the factors of the 2 numbers.
4th Apr 2022, 4:26 PM
Seb TheS
Seb TheS - avatar
+ 1
Seb TheS okay but 1 is common factor in these two numbers how can i declare that.
4th Apr 2022, 5:04 PM
Akash Gupta
Akash Gupta - avatar
+ 1
Lothar Input : 2 3 Output : Co-Prime Input : 4 8 Output : Not Co-Prime
4th Apr 2022, 5:49 PM
Akash Gupta
Akash Gupta - avatar
+ 1
Lothar n=int(input()) l=[] for i in range(2,n+1): if n%i==0: l.append(i) print(l) n=int(input()) m=[] for i in range(2,n+1): if n%i==0: m.append(i) print(m) if list[l]==list[m]: print("co") else: print("not co") Thanks you so much for that approach
5th Apr 2022, 3:06 AM
Akash Gupta
Akash Gupta - avatar