Must be writen in python 3.6.4 1. Simply write the Euclid algorithm in a form th
ID: 3877007 • Letter: M
Question
Must be writen in python 3.6.4
1. Simply write the Euclid algorithm in a form that takes two numbers from input and prints the gcd. The code does not need to check for valid input. It must work for any pair of positive integers.
2. Create a separate program that uses the python timeit module to provide test run-times for your code.
3 Create a third file that implements another algorithm to find the gcd.
4 Provide a program that uses the timeit module to provide sample run-times for this one as well.
Explanation / Answer
import timeit
mc ='''
#3
def Gcd1(n,m):#another function for gcd
a=n
if (n<m):
n=m
m=a
a=m
while(m>0):
if(n%m==0):
if(a%m==0):
return m
m=m-1
#1
def Gcd(n,m):#method to find gcd
if(m==0):
return n
else:
return Gcd(m,n%m)
#prompting and taking input
a=int(input("Enter number1:"))
b=int(input("Enter number2:"))
r=Gcd(a,b)#function calling
print("GCD is: ")
print(r)
r=Gcd1(a,b)#function calling
print("GCD is: ")
print(r)
'''
#2,4
ln = "from math import sqrt"
print (timeit.timeit(setup=ln,stmt =mc,number=5))
output:
Enter number1:10
Enter number2:20
GCD is:
10
GCD is:
10
Enter number1:5
Enter number2:7
GCD is:
1
GCD is:
1
Enter number1:20
Enter number2:45
GCD is:
5
GCD is:
5
Enter number1:20
Enter number2:40
GCD is:
20
GCD is:
20
Enter number1:50
Enter number2:70
GCD is:
10
GCD is:
10
23.39147877333747
>>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.