English subtitles

← Spreading Udaciousness Solution - Intro to Computer Science

Get Embed Code
3 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. So in this problem, we start out with n
    number of people with big heads and happy smiles.
  2. And after every hexamester, each one of
    our happy udacians goes out and asks
  3. spread number of their friends
    to also take the course.
  4. This process continues as long as the
    number of udacians is less than the target.
  5. With a little bit of math, you can solve
    this pretty straightforward,
  6. and you might have done it in
    middle school or high school,
  7. but we're going to use recursion
    to solve this problem very easily.
  8. Remember what we're looking for is the
    number of hexamesters this process goes through
  9. until we reach the target.
  10. I saw on the forms a couple of people
    were trying to use global variables
  11. that counted recursive calls,
  12. but really that kind of defeats the purpose in
    the power that we can use in the recursion.
  13. I'm going to show you how to do that right now.
  14. So our base case says that if we've met our
    target, then we don't need any time to spread
  15. in order to reach the target because
    we're already there.
  16. Now here I have a + 1 because we're
    going to say that we're doing 1 iteration
  17. of the udacian spreading process.
  18. Now for the recursive call.
  19. The first thing we need is n.
  20. Well, n is going to be the same thing as last
    time + how many new people we've gotten,
  21. which is spread x n.
  22. A more succinct way of writing this would
    be simply (n x (1 + spread).
  23. The spreading rate doesn't change,
    so we're going to leave that alone.
  24. Our target doesn't change.
  25. And that's it. We're done.