Question

PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer...

PYTHON

In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides the two numbers without a remainder. For example, the GCD of 8 and 12 is 4. Steps to calculate the GCD of two positive integers a,b using the Binary method is given below: Input: a, b integers If a<=0 or b<=0, then Return 0 Else, d = 0 while a and b are both even do a = a/2 b = b/2 d = d + 1 while a ≠ b do If a%2== 0, then a = a/2 Else If b%2==0, then b = b/2 Else If a > b, then a = (a – b)/2 Else b = (b – a)/2 g = a Return g × 2^d Par(A): Write a function called findGCD that takes two positive integers a, b as parameters and, then calculates and returns the GCD of a, b using the Binary method given above. Part(B): The Least Common Multiple (LCM) of two integers a and b is the smallest positive integer that is divisible by both a and b and can be calculated using the following formula. LCM(a,b)= |ab|/(GCD(a,b)) where, |ab|= Absolute value of a*b GCD(a,b) = Greatest common divisor of a,b Part(C): Let a be any element from the listA and b be any element from the listB. (Note: listA and listB are taken from user) Write a user interface which calculates and displays the Greatest common divisor and Least Common Multiple of each possible pair of elements (a, b) from the two lists.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 from tkinter import * top = Tk() resultStr= StringVar() resultStr.set("Enter Numbers and Click the Button") number1Label = Label (text="First Number") number1Label.pack() number1Box = Entry() number1Box.pack() number2Label = Label (text="Second Number") number2Label.pack() number2Box = Entry() number2Box.pack() resultLabel = Label(textvariable=resultStr) resultLabel.pack() def findGCD(a,b): if a<=0 or b<=0 : return 0 d=0 while a%2==0 and b%2==0: a=a//2 b=b//2 d=d+1 while a!=b: if a%2==0: a=a//2 elif b%2==0: b=b//2 elif a>b: a=(a-b)//2 g=b else: b=(b-a)//2 g=a return g*(2**d) def fun(): no1 = int(number1Box.get()) no2 = int(number2Box.get()) no3 = findGCD(no1,no2) resultStr.set( "The GCD is "+str(no3)) but = Button(text="GCD", command=fun) but.pack() top.mainloop()

from tkinter import *

top = Tk()

resultStr= StringVar()
resultStr.set("Enter Numbers and Click the Button")
number1Label = Label (text="First Number")
number1Label.pack()
number1Box = Entry()
number1Box.pack()

number2Label = Label (text="Second Number")
number2Label.pack()
number2Box = Entry()
number2Box.pack()

resultLabel = Label(textvariable=resultStr)
resultLabel.pack()

def findGCD(a,b):
if a<=0 or b<=0 :
return 0
d=0
while a%2==0 and b%2==0:
a=a//2
b=b//2
d=d+1
while a!=b:
if a%2==0:
a=a//2
elif b%2==0:
b=b//2
elif a>b:
a=(a-b)//2
g=b
else:
b=(b-a)//2
g=a
  
return g*(2**d)

def fun():
no1 = int(number1Box.get())
no2 = int(number2Box.get())
no3 = findGCD(no1,no2)
resultStr.set( "The GCD is "+str(no3))
but = Button(text="GCD", command=fun)
but.pack()

top.mainloop()

Add a comment
Know the answer?
Add Answer to:
PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where...

    1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where a 2 b is equal to the greatest common divisor of b and (a mod b). Write a program that implements this algorithm to find the GCD of two integers. Assume that both integers are positive. Follow this algorithm: 1. Call the two integers large and small. 2. If small is equal to 0: stop: large is the GCD. 3. Else, divide large by...

  • Consider the problem of finding the Greatest Common Divisor (GCD) of two positive integers a and...

    Consider the problem of finding the Greatest Common Divisor (GCD) of two positive integers a and b. It can be mathematically proved that if b<=a GCD(a, b) = b, if (a mod b) = 0; GCD(a, b) = GCD(b, a mod b), if (a mod b) != 0. Write a recursive function called GCD with signature “public static int GCD(int a, int b)” that returns the greatest common divisor of two positive integers a and b with b <= a....

  • I want the code in C++ The greatest common divisor (GCD) of two integers is the...

    I want the code in C++ The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and...

  • IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd)...

    IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an...

  • coding in c programming Functions & Arrays Q1) The greatest common divisor (GCD) of two Integers...

    coding in c programming Functions & Arrays Q1) The greatest common divisor (GCD) of two Integers (of which at least one is nonzero) is the largest positive integer that divides the numbers. Write a C function ged that accepts two integers and returns 1 if both the integers are zero, otherwise it returns their GCD. Write a C program (that includes the function ged) which accepts two integers and prints their GCD. Sample output: Enter two integers: 0 0 At...

  • The least common multiple (lcm) of two positive integers u and v is the smallest positive...

    The least common multiple (lcm) of two positive integers u and v is the smallest positive integer that is evenly divisible by both u and v. Thus, the lcm of 15 and 10, written lcm (15,10), is 30 because 30 is the smallest integer divisible by both 15 and 10. Write a function lcm() that takes two integer arguments and returns their lcm. The lcm() functon should calculate the least common multiple by calling the gcd() function from program 7.6...

  • 9. The following C-like code calculates the greatest common divisor (GCD) of the two 8-bit positi...

    9. The following C-like code calculates the greatest common divisor (GCD) of the two 8-bit positive integers a and b (Aside: This is Euclid's algorithm from 300 BC). Complete the HLSM for the code (Answers are case sensitive) Inputs: byte a, byte b, bit go Outputs: byte gcd, bit done GCD while (1) ( while (!go); done 0 while (a!-b){ if(a>b){ a-a b else gcd-a done 1 Inputs: go (bit), a, b (8 bits) Outputs: done (bit), ged (8 bits)...

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g...

    Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g edi ) and gcdr , which both take two integers a, b and calculates their greatest common divisor (GCD) using the Euclidean algorithm gcdi () should do so using iteration while gcdr () should use recursion. Then write a third function, gcd(), which takes two integers a, band an optional third argument nethod which takes a charater string containing either "iterative" or "recursive", with...

  • a Find the greatest common divisor (gcd) of 322 and 196 by using the Euclidean Algorithm....

    a Find the greatest common divisor (gcd) of 322 and 196 by using the Euclidean Algorithm. gcd- By working back in the Euclidean Algorithm, express the gcd in the form 322m196n where m and n are integers b) c) Decide which of the following equations have integer solutions. (i) 322z +196y 42 ii) 322z +196y-57

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT