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
What value of x makes the equation sin x=cos43​
g100num [7]

Answer:

0.55511330152

Step-by-step explanation:

5 0
4 years ago
Read 2 more answers
Simplify the expression 11x − (2x2 + 8x).
34kurt
11x - (2x^2 + 8x) 

11x-2x^2-8x 

(11x-8x)-2x^2 

3x-2x^2
5 0
4 years ago
Please help I have to turn this in by the end of the night
Zina [86]

Answer:

A.) What is your favorite subject this year?

Step-by-step explanation:

A statistical question is a type of question that can't be answered with just one number or value. It has to have different responses for different types of people in different situations. An example of a statistical question is "What do you do afterschool?" because every person does different things afterschool. The only question that has different responses for different people is the first one, "What is your favorite subject this year?".

3 0
3 years ago
Read 2 more answers
Find the value of a.<br> 4x + ax = - 2x(2x + 1)
Karo-lina-s [1.5K]

Answer:

a = -2(2x+1)-4

Step-by-step explanation:

1. Factor out the common term x.

x(4+a)= -2x(2x+1)

2. Cancel x on both sides.

4+a= -2(2x+1)

3. Subtract 4 from both sides.

a= -2(2x+1)-4

3 0
3 years ago
What is the product of -8 and -7
KATRIN_1 [288]

Answer:56

Step-by-step explanation: -8x-7=56

7 0
3 years ago
Read 2 more answers
Other questions:
  • Carisa predicted she answered 9 questions correctly out of 10 on a test. She actually got 8 answers correct. By what percent was
    14·1 answer
  • Which expression shows the simplified form of (8 r Superscript negative 5 Baseline) Superscript negative 3? 8 r Superscript 15 S
    10·2 answers
  • write an algebraic expression for the following word phrase the quotient of r and 12... a. r • 12 b. r 12 c. r ÷ 12 d. r – 12
    14·1 answer
  • What does 68.8333333 round to in the nearest tenth?
    11·1 answer
  • Please help me :(((((
    14·1 answer
  • Find the slope of the direct variation y=3/4x
    5·1 answer
  • If $10,000 is borrowed at an APR of 18%, for a period of 5 years and payments are made monthly, what will be the monthly payment
    5·1 answer
  • What are the slope and the y-intercept of the linear function that is represented by the equation y = 9 x minus 2?
    11·2 answers
  • That all they gave me
    7·1 answer
  • Plz help asap don’t get at all
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!