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
Mias dog weighs 32.6 pounds. Letties dog weighs 3.8 times as much as Mias you. What does Letties dog weigh in pounds?
Whitepunk [10]
Hello!

We are given the weight of Mia's dog and told to find the weight of Lettie's dog based on the known relationship. Using these given values, we can create the following equation (let "w" represent the total weight of Lettie's dog):

w = 32.6(3.8)

Simplify the right side of the equation:

w = 123.88

We have now proven that Lettie's dog weighs 123.88 lbs.

I hope this helps!
6 0
3 years ago
Read 2 more answers
Help me pls. Pls do this step by step. I will give he brainliest to the person who does it step by step.
Zanzabum

Answer:

x^2 - x^3 - 3x^2y - 3xy^2

Step-by-step explanation:

x^3 +y^3 - (x + y)^3​

Expand the expression

x^2 + y^3 - (x^3 + 3x^2y + 3xy^2 +y3)

Remove the parentheses

x^2 +y^3 -x^3 -3x^2y -3xy^2 - y^3

Remove the opposites

Answer:

x^2 - x^3 - 3x^2y - 3xy^2

Hope this Helps!

3 0
3 years ago
what is an equation of the line that is parallel to the line with y=-.60x+2 and passes through the point (10,-10)
sp2606 [1]

y = -0.60x - 4

because the slope stays the same because it is parallel and the y intercept for the new equation ends up being -4

4 0
3 years ago
How much should you put in an investment paying a simple interest rate of​ 5% if you needed​ $4000 in eighteen​ mont
Eddi Din [679]

now, there are 12 months in a year, so 18 months is really 18/12 of a year, thus

~~~~~~ \textit{Simple Interest Earned Amount} \\\\ A=P(1+rt)\qquad \begin{cases} A=\textit{accumulated amount}\dotfill & \$4000\\ P=\textit{original amount deposited}\\ r=rate\to 5\%\to \frac{5}{100}\dotfill &0.05\\ t=years\to \frac{18}{12}\dotfill &\frac{3}{2} \end{cases} \\\\\\ 4000=P[1+(0.05)(\frac{3}{2})]\implies 4000=P(1.075) \\\\\\ \cfrac{4000}{1.075}=P\implies 3720.93\approx P

6 0
2 years ago
Round the quantity to th given place value 13.989 to the nearest tenth
Aliun [14]

Answer:

13.989 rounded to nearest tenth is 14.0.

Step-by-step explanation:

3 0
2 years ago
Read 2 more answers
Other questions:
  • the tickets for a dance recital cost $5.00 for adults and $2.00 for children. if the total number of tickets sold was 295 and th
    14·1 answer
  • Pls help due right now!
    13·2 answers
  • The figure below shows parallel lines cut by a transversal:
    14·2 answers
  • Line l and Line m are parallel lines that have been rotated 180°. The resulting images are show as l' and m'. How can you descri
    11·1 answer
  • Explain how to form a linear combination to eliminate the variable y for this system 2x-3y=3 5x+2y=17
    11·2 answers
  • A spinner used for a game is divided into 3 sections: yellow, green, and orange. Janie spins the spinner 20 times. The spinner l
    10·2 answers
  • The number of users of social media has increased significantly since the year 2001. In fact, the approximate number of users ha
    12·1 answer
  • On Alex's map, 0.5 inch represents 20 miles.
    7·1 answer
  • A garden hose is 15 yards long. What is the length of the hose in inches?
    13·2 answers
  • HELP
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!