1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
MariettaO [177]
2 years ago
11

5. The recursive algorithm given below can be used to compute gcd(a, b) where a and b are non-negative integer, not both zero.

Mathematics
1 answer:
s2008m [1.1K]2 years ago
6 0

Implementating the given algorithm in python 3, the greatest common divisors of <em>(</em><em>124</em><em> </em><em>and</em><em> </em><em>244</em><em>)</em><em> </em>and <em>(</em><em>4424</em><em> </em><em>and</em><em> </em><em>2111</em><em>)</em><em> </em>are 4 and 1 respectively.

The program implementation is given below and the output of the sample run is attached.

def gcd(a, b):

<em>#initialize</em><em> </em><em>a</em><em> </em><em>function</em><em> </em><em>named</em><em> </em><em>gcd</em><em> </em><em>which</em><em> </em><em>takes</em><em> </em><em>in</em><em> </em><em>two</em><em> </em><em>parameters</em><em> </em>

if a>b:

<em>#checks</em><em> </em><em>if</em><em> </em><em>a</em><em> </em><em>is</em><em> </em><em>greater</em><em> </em><em>than</em><em> </em><em>b</em>

return gcd (b, a)

<em>#if</em><em> </em><em>true</em><em> </em><em>interchange</em><em> </em><em>the</em><em> </em><em>Parameters</em><em> </em><em>and</em><em> </em><em>Recall</em><em> </em><em>the</em><em> </em><em>function</em><em> </em>

elif a == 0:

return b

elif a == 1:

return 1

elif((a%2 == 0)and(b%2==0)):

<em>#even</em><em> </em><em>numbers</em><em> </em><em>leave</em><em> </em><em>no</em><em> </em><em>remainder</em><em> </em><em>when</em><em> </em><em>divided</em><em> </em><em>by</em><em> </em><em>2</em><em>,</em><em> </em><em>checks</em><em> </em><em>if</em><em> </em><em>a</em><em> </em><em>and</em><em> </em><em>b</em><em> </em><em>are</em><em> </em><em>even</em><em> </em>

return 2 * gcd(a/2, b/2)

elif((a%2 !=0) and (b%2==0)):

<em>#checks</em><em> </em><em>if</em><em> </em><em>a</em><em> </em><em>is</em><em> </em><em>odd</em><em> </em><em>and</em><em> </em><em>B</em><em> </em><em>is</em><em> </em><em>even</em><em> </em>

return gcd(a, b/2)

else :

return gcd(a, b-a)

<em>#since</em><em> </em><em>it's</em><em> </em><em>a</em><em> </em><em>recursive</em><em> </em><em>function</em><em>,</em><em> </em><em>it</em><em> </em><em>recalls</em><em> </em><em>the function</em><em> </em><em>with </em><em>new</em><em> </em><em>parameters</em><em> </em><em>until</em><em> </em><em>a</em><em> </em><em>certain</em><em> </em><em>condition</em><em> </em><em>is</em><em> </em><em>satisfied</em><em> </em>

print(gcd(124, 244))

print()

<em>#leaves</em><em> </em><em>a</em><em> </em><em>space</em><em> </em><em>after</em><em> </em><em>the</em><em> </em><em>first</em><em> </em><em>output</em><em> </em>

print(gcd(4424, 2111))

Learn more :brainly.com/question/25506437

You might be interested in
The diameter of a small gear is 16 cm. This is 2.5 cm more than 1/4 of the diameter of a larger gear. What is the diameterof the
Alex787 [66]
16 cm - 2.5 cm = 13.5 cm, which is 1/4 of the diameter of the large gear. Take 13.5 cm * 4, and you have the diameter of the large gear, which is 54 cm.
6 0
3 years ago
Read 2 more answers
Help ASAP PLEASE! I'll mark brainliest
maksim [4K]

Answer:

16

Step-by-step explanation:

8 0
3 years ago
Read 2 more answers
Could someone help me with math please and please txt back
Pani-rosa [81]
What do you need help on?

3 0
3 years ago
A stadium already has 250 people in it at noon.If 35 people per minute enter the stadium,how many are in the stadium at 1:30 p.M
Talja [164]
12:00 to 1:30 , that is 90 minutes
if 35 people enter each minute then it should be multiplied by the number of minutes
that being said, 90 x 35 = 3,150 people are in the stadium at 1:30
3 0
3 years ago
SOS HELPPPP
Hunter-Best [27]

Answer:

18

Step-by-step explanation:

5 0
2 years ago
Other questions:
  • During the shot put event, Alayna throws the ball 32.45 feet while her teammates throw 28.34 feet, 26.59 feet, and 33.11 feet. U
    13·2 answers
  • How to make a business tycoon?
    9·1 answer
  • Find three fractions whose product is -5/24.
    10·1 answer
  • Can a triangle be formed if the side<br> lengths are 10, 23, and 10?
    15·2 answers
  • One of the smallest pools on the ship has about 45,000 gallons of water in it.
    11·1 answer
  • Is 9.63 &lt; or &gt; than 1.82? And how do you tell
    15·1 answer
  • Someone give the worked solution for this please!!!!
    7·1 answer
  • There are 20 marbles in a bag. 35% of the marbles are blue. how many marbles are blue?
    8·2 answers
  • If TWO^2 = THREE where the alphabets are single-digit integers then find T + W + O​
    5·1 answer
  • Some body help me out??
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!