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
What is an example of the commutattive property of multiplication
avanturin [10]
Example:3x2=2x3 !!!!!!!!!!!!
4 0
3 years ago
Read 2 more answers
I need help please and thank you
Greeley [361]
4 is 120 apples
5 is 30 pounds of ice
5 0
3 years ago
Read 2 more answers
What is 9 7/8 as a pecent
JulsSmile [24]
9 and 7/8 as a precent is 0.875 or 87%
4 0
3 years ago
Given that these simultaneous equations
Tatiana [17]

The value of k is(3\sqrt{ 10})/2

<h3>How to solve the simultaneous equation?</h3>

Given:

x-y=k.............(eq i)

2x²+y²-15..............(eq ii)

We would make y the subject formula in eq ii

2x²+y²-15= 0

2x² + y²= 15

y²= 15-2x²

y= \sqrt{15-2x^2}...........(eq iii)

Substitute the value of y into eq i

x-(\sqrt{15-2x^2}= k

x- (\sqrt{15} - 2x= k

k= (3\sqrt{ 10})/2

Read more about simultaneous equations here:

brainly.com/question/16863577

#SPJ1

3 0
2 years ago
An investment pays 4.6% annual interest compounded monthly. If $3100 is
elena55 [62]

Answer:

I think it's 2566.8 if 4.6 is taken out of the account annually. I think that is what it is asking.. I'm only 13 so I'm not great at these yet but it should be right because I'm really good at math

Step-by-step explanation:

3 0
3 years ago
Other questions:
  • What is 732 in word form?
    7·2 answers
  • Give two representations in polar coordinates for the rect- angular point (−3,−3√3)
    5·1 answer
  • 6 inches of snow in 5 hours into a rate
    10·1 answer
  • Ernesto tried to determine the solution for the system of equations using substitution. His work is shown below.
    8·2 answers
  • How many 5 person squads can a basketball coach send out on an 11 person team
    5·2 answers
  • The sum of the measures of a complement and a supplement of an angle is 200. What is the measure of the angle?
    10·1 answer
  • Your drawer contains 10 red socks and 7 blue socks. You pick 3 socks without replacement. What's the probability that at least t
    5·1 answer
  • 10 mi<br> 6 mi<br> b<br> What is the length of the missing leg?<br> b<br> miles
    7·1 answer
  • Plizzz answer asap!!!​
    5·1 answer
  • What is the greatest common factor of the terms in the polynomial 12x4 + 27x3 + 6x2?
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!