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
- Set g :=greater of a and b; l := the lesser.
- Find the unique q, r for which g = lq + r by the division algorithm.
- If r = 0, then the GCD is l, and should be returned
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started