ShmoopTube
Where Monty Python meets your 10th grade teacher.
Search Thousands of Shmoop Videos
AP Computer Science 3.1 Standard Algorithms 176 Views
Share It!
Description:
AP Computer Science 3.1 Standard Algorithms. What is the output of Recurse(3)?
Transcript
- 00:00
Sorry And here's your smoked use your brought to you
- 00:05
by rijker zhan brought to you by rekers jin Brought
- 00:09
to you by rekers asian brought to you by consider
- 00:14
the following code segment What is the output of re
- 00:17
curse three Ah we're thinking all right And here the
Full Transcript
- 00:22
mental answers on numbers Okay this question Assessing what we
- 00:28
know about rikers in which is essentially having a method
- 00:31
call itself This creates a type of loop that goes
- 00:34
round and round around until some condition typically called the
- 00:37
base case is satisfied like a dog chasing its tail
- 00:40
in telling finally catches it a simple implementation of riker
- 00:43
shin might look something like this All right well this
- 00:47
method would take an imager And if it's greater than
- 00:50
zero print hello plus the imager Then it will decrease
- 00:54
the imager by one and call itself again And if
- 00:58
it's still greater than zero it'll do it again can
- 01:00
again and again it'll rikers decreasing over and over and
- 01:05
saying hello annie in inger goes up there until it
- 01:08
reaches zero go at zero it will simply return the
- 01:11
zero and the method will no longer call itself it's
- 01:15
a bit like the recursive algorithm shown in this question
- 01:17
except this one is also doing some funky math to
- 01:20
the imager so we'll need to carefully plot out what's
- 01:22
happening step by step here's our sniffing and the question
- 01:26
is asking pre output of re curse free All right
- 01:30
well starting at the top we're getting our method ready
- 01:32
with the parameter into end which will let us accept
- 01:35
that three and play with it as variable end when
- 01:38
rikers three gets called if it is less than one
- 01:42
will return the number one doesn't apply to us at
- 01:44
the moment though else will return one plus the product
- 01:48
of three times re curse and minus one better known
- 01:51
as to wait a min We're trying to calculate what
- 01:55
happens in rikers right now How can we use something
- 01:58
that hasn't been calculated yet Has a factor in our
- 02:01
calculation Shouldn't they shatter the universe somehow Well we're just
- 02:05
gonna have to dive into the first method again and
- 02:07
see how deep the rabbit hole goes at this point
- 02:09
Imager and is currently three and we're being asked for
- 02:12
re curse and minus one so it's just like calling
- 02:16
re cursed too We did The fancy man if end
- 02:20
now to remember is less than one return One else
- 02:24
return one plus three times rikers and minus one So
- 02:29
we'll be calling recruits again this time as reversed one
- 02:32
so it ends less than one return One else returned
- 02:35
one plus three times were cursed and minus one and
- 02:38
calling rikers yet again this time as rikers Zero Ah
- 02:42
but here we go If and is less than one
- 02:45
which it is We return one The product of rikers
- 02:48
zero is one We've stopped the lute e Now we
- 02:53
just have to climb back out of this home were
- 02:54
in back to rikers one We now know that riker
- 02:57
zero equals one So occurs one equals one plus the
- 03:01
product of three times one well in california three times
- 03:04
One two three and add one to that for four
- 03:07
So recruits one equals four and climbing up a rung
- 03:10
refers to it three times for his twelve plus one
- 03:14
is thirteen to rikers to is thirteen And at rikers
- 03:18
three three times thirteen is thirty nine plus one which
- 03:21
is forty So the product of rikers three is forty
- 03:24
Who A lot of work Someone really should write a 00:03:26.9 --> [endTime] computer program to handle it for us next time
Up Next
AP Computer Science 1.4 Standard Algorithms. How many times will mystery be called for mystery(n) for n > 1?
Related Videos
AP Computer Science 4.2 Standard Algorithms. What kind of algorithm is the following?
AP Computer Science 1.2 GridWorld Case Study and APIs. What is the direction of the actor?
AP Computer Science 2.3 Classes and Objects. Which of the following is correct implementation of the Country class?
AP Computer Science 3.4 Inheritance, Abstraction, and Polymorphism. Which of the following will satisfy the conditional if statement for boo, str,...