Timeline for answer to Need some help to understand recursion by jaggedsoft
Current License: CC BY-SA 3.0
Post Revisions
15 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 13, 2017 at 12:48 | history | edited | URL Rewriter Bot |
replaced http://cs.stackexchange.com/ with https://cs.stackexchange.com/
|
|
| Nov 17, 2015 at 11:34 | vote | accept | Community Bot | ||
| Nov 17, 2015 at 9:02 | history | edited | jaggedsoft | CC BY-SA 3.0 |
deleted 118 characters in body
|
| Nov 17, 2015 at 8:55 | comment | added | jaggedsoft |
@qtxt: Posted another example. It's important to understand that if you are calling sumTo(4) that the function will be called a total of four times. It can be tricky. I hope you get it figured out. Good luck!
|
|
| Nov 17, 2015 at 8:53 | comment | added | user824425 |
@NextLocal That's not an implementation of C's and JavaScript's standard pow, which take a double for the exponent. pow is, with rare exceptions, implemented in terms of exp and log, which in turn are usually implemented in hardware, where they have an iterative structure rather than a recursive one.
|
|
| Nov 17, 2015 at 8:51 | history | edited | jaggedsoft | CC BY-SA 3.0 |
added example
|
| Nov 17, 2015 at 8:39 | history | edited | jaggedsoft | CC BY-SA 3.0 |
added example
|
| Nov 17, 2015 at 8:28 | history | edited | jaggedsoft | CC BY-SA 3.0 |
deleted 350 characters in body
|
| Nov 17, 2015 at 8:25 | comment | added | user4851524 | @NextLocal: Well, I found some example, which seems understandable, the first block of code here: integralist.co.uk/posts/js-recursion.html It's clear that everytime x is increasing, and y is decreasing, so when we finally call sum (11, 0), y > 0 is false and it just returns x, which is 11. But when there is only one variable it seems much trickier, so I still have troubles despite all efforts. | |
| Nov 17, 2015 at 8:08 | comment | added | jaggedsoft | @qtxt: The original function calls a child function, which in return calls another child function. And none of the functions can complete until it reaches an exit condition where it's finally returning an an actual value. Recursive functions can be hard to understand, it's like the movie inception. I'll update my post with another example, hope it helps. Otherwise, let me know. | |
| Nov 17, 2015 at 7:59 | comment | added | jaggedsoft |
This recursive pow function has a time complexity of O(logb) I'd be curious if you could find a faster one: pow(a,b) { if(b==0) return 1 temp = pow(a,b/2) if(b%2==0) return(temp * temp) else return(a * temp * temp) }
|
|
| Nov 17, 2015 at 7:44 | comment | added | user824425 |
pow() is not recursive, or at least not noticeably so.
|
|
| Nov 17, 2015 at 7:39 | comment | added | user4851524 | I just need to follow this step-by step. So, if each function waits for n everytime, we have the following: 3 > 1, so we return 3 + sumTo(n - 1), and wait for the next sumTo(), right? It looks like next sumTo() deals with 2, so sumTo(2) waits for next sumTo(), which is sumTo(1). 1 > 1 is false, so last function returns 1, but where does it go? | |
| Nov 17, 2015 at 7:28 | history | edited | jaggedsoft | CC BY-SA 3.0 |
added examples
|
| Nov 17, 2015 at 7:19 | history | answered | jaggedsoft | CC BY-SA 3.0 |