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
I will brainlist you awnser
77julia77 [94]

Answer:

16

Step-by-step explanation:

5 0
3 years ago
HELP NEEDED , YOU WILL GET 10 POINTS !
Nitella [24]

Answer:

stop cheating on your schoology test;)

Step-by-step explanation:

8 0
3 years ago
A cannon ball is launched at 6 feet per second from the top of a platform 12 feet above the ground.
AleksAgata [21]
A.) y = ut - 1/2gt^2 + 12
y = 6t - 1/2(9.8)t^2 + 12
y = 6t - 4.9t^2 + 12

b.) The y-intercept is 12

c.) The y-intercept represent the height of the ball at time t = 0

d.) At the time the ball reaches the ground, y = 0
-4.9t^2 + 6t + 12 = 0
t = 2.29 seconds
Therefore, it took the ball 2.29 seconds to reach the ground.
7 0
3 years ago
Ali hops on his 3 wheeler and heads off at 12 mph. Two hours later, Fatimah chases after him on a moped at 18 mph. How far will
mash [69]

Answer: 72 miles.

Step-by-step explanation:

We know the relationship:

Distance = speed*time.

Then we can write the equation for distance as a function of time for Ali as:

A(t) = 12mph*t

where t is time in hours.

Fatimah's equation will be:

F(t) = 18mph*(t - 2h)

where the -2h appears because she starts two hours after Ali.

Fatimah will overtake Ali when F(t) = A(t) (their positions are the same)

Then we need to solve:

12mph*t = 18mph*(t - 2h)

12mph*t = 18mph*t - 18mph*2h

12mph*t = 18mph*t - 36 mi

36 mi = (18mph - 12mph)*t

36mi = 6mph*t

36mi/6mph = t

6h = t

So Ali travels for 6 hours before he gets overtaken, then the total distance that Ali travels is:

A(6h) = 12mph*6h = 72 mi

3 0
2 years ago
HELPPP PLEASE!!! I’M BEING TIMED!!!!
Evgesh-ka [11]

Answer:

y= -7x -27

Step-by-step explanation:

5 0
3 years ago
Other questions:
  • Find the value of -7 + (-9) + 11 + 8.
    10·1 answer
  • Read the excerpt from Into the Unknown by Stewart Ross, and then look at the image below.
    14·2 answers
  • Can you please help me with this problem? It shouldn't be to hard if you've done Algebra one.
    7·1 answer
  • Pam’s eye-level height is 324 ft above sea level, and Adam’s eye-level height is 400 ft above sea level. How much farther can Ad
    9·2 answers
  • a bowling league requires you to buy a bowling ball for $45 and pay $12 for each game. write and solve an equation to determine
    12·1 answer
  • Please someone help me ASAP
    8·2 answers
  • What is the sign of p<br> a<br> when p &gt; 0 and a<br> р<br> 0?<br> .
    5·1 answer
  • Place each job in order from highest to lowest by reading each description. Think about other civiliation
    7·1 answer
  • PLEASE CAN SOMEONE HELP I HAVE 5 mins
    11·1 answer
  • I need help please help with this question I will give brainliest this is a major grade so no joking around please
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!