Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions