Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Devise a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b with using the fact that gcd (a,b) = gcd (a,b - a) .

Short Answer

Expert verified

The recursive algorithm is,

proceduregcd(a,b:positiveintegerswitha<b)ifa=0thenreturnbelseifa=b-athenreturnaelseifa<b-athenreturngcd(a,b-a)elsereturngcd(b-a,a)

Step by step solution

01

Describe the given information

The objective is to write the recursive algorithm for finding the greatest common divisor of two nonnegative integers a and b.

02

Give a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b

Call the algorithm "greatest divisor" and the input are two positive integers and with a < b .

proceduregcd(a,b:positive integers witha<b)

When one of the integers is zero, then the greatest common divisor is the other integer.

if a = 0 then

return b

Since gcd (a,b = gcd (a,b-a) when is nonzero.

elseifa=b-athenreturnaelseifa<b-athenreturngcd(a,b-a)elsereturngcd(b-a,a)

By combining all the steps, the required algorithm will be as follows.

proceduregcd(a,b:positiveintegerswitha<b)ifa=0thenreturnbelseifa=b-athenreturnaelseifa<b-athenreturngcd(a,b-a)elsereturngcd(b-a,a)

Therefore, the recursive algorithm is shown above.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free