Using this code, write a flow chart that describes what this code does. Also, wr
ID: 3768784 • Letter: U
Question
Using this code, write a flow chart that describes what this code does. Also, write another flow chart showing how the Euclidian Algorithm calculates the GCD. Then, write the program.
def _factor(n,m):
n_factor=[]
for i in range(n,0,-1):
if n % i == 0:
print(n,i,n/i)
n_factor.append(n/i)
m_factor=[]
for i in range(m,0,-1):
if m % i == 0:
print(m,i,m/i)
m_factor.append(m/i)
print(n_factor,m_factor)
com_factor=[]
for a in n_factor:
for b in m_factor:
if a==b:
com_factor.append(a)
print("GCD is: " , max(com_factor))
n=int(input("Enter first number: "))
m=int(input("Enter first number: "))
_factor(n,m)
Explanation / Answer
First it find the Common Factor of both number (n and m).
Then from the list of Common Factor choose the Commom Factor with maximum value,
For n = 36 and m = 24, we have flow chart like this
(36, 36, 1)
(36, 18, 2)
(36, 12, 3)
(36, 9, 4)
(36, 6, 6)
(36, 4, 9)
(36, 3, 12)
(36, 2, 18)
(36, 1, 36)
(24, 24, 1)
(24, 12, 2)
(24, 8, 3)
(24, 6, 4)
(24, 4, 6)
(24, 3, 8)
(24, 2, 12)
(24, 1, 24)
([1, 2, 3, 4, 6, 9, 12, 18, 36], [1, 2, 3, 4, 6, 8, 12, 24])
=> Commom Factors are -> 1,2,3,4,6,12
=> ('GCD is: ', 12)
Euclid Function is like ->
def _euclid(a,b):
if (a == 0):
print 'GCD IS -> ',b
return
print "_euclid("+str(b%a) + ',' + str(a) + ")"
_euclid(b%a,a)
n=int(input("Enter first number: "))
m=int(input("Enter first number: "))
_euclid(n,m)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.