Question: Problem 1 - Implement the Euclidean Algorithm As discussed in class, the Euclidean algorithm finds the greatest common divisor of two integers a and b.

Problem 1 - Implement the Euclidean Algorithm

As discussed in class, the Euclidean algorithm finds the greatest common divisor of two integers a and b. As a refresher, the algorithm runs roughly as follows

  1. Set g :=greater of a and b; l := the lesser.
  2. Find the unique q, r for which g = lq + r by the division algorithm.
    1. If r = 0, then the GCD is l, and should be returned
    2. Otherwise, set g := l and l := r; repeat from step 2

This is the code we're supposed to follow in CoCalc (like using sage online):

def gcd(a,b): g = max(a,b) l = min(a,b) q = g // l # g // l = greatest integer less than g / l r = g % l # g % l = least positive integer congruent to g (mod l) (least positive reside) while True: # Change `while True:` to a proper loop-condition pass # Replace 'pass' with your code return 1 # Replace '1' with a return value

This is what I have so far, but I keep getting syntax errors. I can't figure out how to set up my "While" loop:

def gcd(a,b): g=max(a,b) l=min(a,b) q= g//1 r= g %1 While g=lq+r : If r==0 then gcd==l Print(l) Else g==l and l==r Return g and l

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!