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]
3 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]3 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
Which graph represents the inverse sine function?
hichkok12 [17]
Answer: A.
The inverse sine function is written as sin−1(x) or arcsin(x). Inverse functions swap x- and y-values, so the range of inverse sine is − π/2 to π/2 and the domain is −1 to 1. When evaluating problems, use identities or start from the inside function.
(REFER TO CHART BELOW)

6 0
2 years ago
How do you reduce 6 33/16
UkoKoshka [18]
When you see the fraction 33/16, you realize that 33 divided by 16 is 2 remainder 1, so you add the 2 to the original 6, then put the remainder 1 on top of 16 ,which gives you 8 1/16
3 0
3 years ago
Read 2 more answers
Benito spent $1691 to operate his car last year. Some of these expenses are listed below. Benito's only other expense was for ga
ryzh [129]
D . it doesn't say how much he spent on gas 
5 0
3 years ago
Read 2 more answers
Will give brainliest if answer is correct!
xeze [42]

A quadratic always has two solutions. They could be two real number solutions (the parabola crosses the x-axis in two places), one real number double solution (the parabola just touches the x-axis in one spot) to two complex (imaginary) solutions where the parabola doesn't cross the x-axis.

Please give me brainliest

6 0
3 years ago
Read 2 more answers
Which transformations could have occurred to map ABC
antoniya [11.8K]
A translation and dilation
5 0
3 years ago
Other questions:
  • A rectangle is 3 times as long as it is wide, and its perimeter is 80 centimeters. Find its dimensions
    12·1 answer
  • the average number of spectators at a football competition for the first five days was 3144.the attendance on the sixth day was
    5·1 answer
  • Julia bought name tags for a party. Each name tag is 3 3/4 in. wide and 2 1/3 in. high.
    7·2 answers
  • MARKING BRAINLIEST AND GIVING UP TO 100 POINTS FOR BEST ANSWER!!! ALL OTHERS WILL BE DELETED IF IT IS A SCAM!!!
    9·2 answers
  • HELP
    12·1 answer
  • Formula to find perimeter of rectangle
    8·2 answers
  • What fraction is equal to -8/24
    10·2 answers
  • Calculate the average rate of change for the function
    5·1 answer
  • HELP ASAP! GIVING BRAINLIEST TO BEST ANSWERS FOR THESE 2 QUESTIONS!
    6·2 answers
  • The value of the 4th term is 80. The sequence is doubling at each step. (Please answer this is due tomorrow)
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!