Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1. As discussed in class, Euclid's GCD algorithm is slow. The plain English description of a faster GCD algorithm is as follows: Repeatedly replace

image text in transcribed
Problem 1. As discussed in class, Euclid's GCD algorithm is slow. The "plain English" description of a faster GCD algorithm is as follows: Repeatedly replace the larger of the two numbers by its remainder when divided by the smaller of the two. This algorithm terminates when the remainder is 0 . This means that the smaller number evenly divides the larger number. When this happens the smaller number is returned as the GCD of the two original input numbers. Example: Suppose the input is 18 and 32 Then the algorithm repeats 4 times. In the first iteration, the remainder when 32 is divided by 18 is 14 . So 32 is replaced by 14 and the two numbers that enter the second iteration are 14 and 18 . This continues for two more iterations (as shown in the table above), at which point the numbers we have are 2 and 4 . When 4 is divided by 2 , the remainder is 0 . So the algorithm terminates and the GCD returned by the algorithm is 2 . A more precise description of the algorithm in psedocode is as follows: INPUT: x,y 1. big max(x,y) 2. small min(x,y) 3. rem . the remainder when big is divided by small 4. As long as rem is not , repeat: 5. big small 6. small rem 6. QUTPUT small Note that the remainder, when big is divided by small, is going to be smaller than small. This explains the assignments in Lines 5-6. Write a Python function called fastGCD that implements the above algorithm. The signature of the function should be deffastcco(x,y): The function should return the GCD of x and y, when x and y are both positive integers. Advice: (i) Start with the function EuclidGCD that has been posted and edit it to get fastGCD, (ii) \& is the remainder operator in Python (e.g., 1083 will evaluate to 1), (ii) max and min are pre-defined functions in Python

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Database Security Applications And Trends

Authors: Michael Gertz, Sushil Jajodia

1st Edition

1441943056, 978-1441943057

More Books

Students also viewed these Databases questions

Question

Perform viability testing using the economics of one unit.

Answered: 1 week ago