PROBLEM 50: GCD of 2 numbers using Middle School Algorithm approach
(For best view experience, view in windows version)
Author : Ajay ZadDate    : 02/07/2022
m = int(input("Enter the M number : "))n = int(input("Enter the N number : "))p = []
#Finding Prime numbers for i in range(2,20):    cnt = 0    for j in range(2,i):        if i % j == 0:            cnt = cnt + 1    if cnt == 0:        p.append(i)
#Finding prime factors of Ml = []a = 0while m >= 1:    if a == len(p) - 1:        break    if m % p[a] == 0:        l.append(p[a])        m = m / p[a]    else:        a = a + 1print("Factors of number M : ",l)      
 #Finding prime factors of N        s = []b = 0while n >= 1:    if b == len(p) - 1:        break    if n % p[b] == 0:        s.append(p[b])        n = n / p[b]    else:        b = b + 1print("Factors of number N :",s)
#Finding common factors of M & Nm = []for i in p:    if i in l and i in s:        ll = l.count(i)        ss = s.count(i)        mi = min(ll,ss)        pro = 1        for j in range(0,mi):            pro = pro * i        m.append(pro)        pro = 1for i in m:    pro = pro * i    print("GCD of M and N = ",pro)    
The above program is in Python language
Output : 
Enter the M number : 32 Enter the N number  : 80
Factors of number M :  [2, 2, 2, 2, 2]Factors of number N  :  [2, 2, 2, 2, 5]
GCD of M and N =  16
m = int(input("Enter the M number : "))
n = int(input("Enter the N number : "))
p = []
#Finding Prime numbers 
for i in range(2,20):
    cnt = 0
    for j in range(2,i):
        if i % j == 0:
            cnt = cnt + 1
    if cnt == 0:
        p.append(i)
#Finding prime factors of M
l = []
a = 0
while m >= 1:
    if a == len(p) - 1:
        break
    if m % p[a] == 0:
        l.append(p[a])
        m = m / p[a]
    else:
        a = a + 1
print("Factors of number M : ",l)      
#Finding prime factors of N        
s = []
b = 0
while n >= 1:
    if b == len(p) - 1:
        break
    if n % p[b] == 0:
        s.append(p[b])
        n = n / p[b]
    else:
        b = b + 1
print("Factors of number N :",s)
#Finding common factors of M & N
m = []
for i in p:
    if i in l and i in s:
        ll = l.count(i)
        ss = s.count(i)
        mi = min(ll,ss)
        pro = 1
        for j in range(0,mi):
            pro = pro * i
        m.append(pro)
pro = 1
for i in m:
    pro = pro * i
print("GCD of M and N = ",pro)
The above program is in Python language
Output : 
Enter the M number : 32 
Enter the N number  : 80
Factors of number M :  [2, 2, 2, 2, 2]
Factors of number N  :  [2, 2, 2, 2, 5]
GCD of M and N =  16
 
No comments:
Post a Comment