Sunday, March 2, 2014

Week 7: Recursion

Recursion is something that requires quite a bit thoughts and every time you think about it deeply, you will get a better understanding about it. We have been learning recursion since the fourth week and I am confident about designing recursion functions now although I was very confused about it(which can be noticed in my previous slog post).

Here, I'm sharing my own trick about designing recursion functions.
Firstly,you have to assume that the function you're writing is already defined. For example, if you wanna write for the n-th case, you will have to assume that the previous cases have already been defined somehow and you can use them by calling the function itself whenever you want. Finally, check that whether your function has base case. If it does, leave it, you're done. If not, add a base case for your function to know when to exit the recursion. Usually, we use if statement to add base cases, but that's not always the case.

I found a very interesting website "http://projecteuler.net/", where you can find some nice programming practice. And I practiced recursion on it by writing the fibonacci sequence. Here I'm sharing my code.

Now, I'm really concerned about the efficiency of recursive functions because it always takes a lot of time for a recursion function to execute. Although some functions can only be written using recursive structure, there are many other functions that can be written in simple and regular ways. I hope we will learn more about how to improve the efficiency of our program later on.

I wanna share a word from one of the slogs from my classmates at the end--------------"Recursion---my worst enemy and my best friend".



2 comments:

  1. Yeah, I totally agree with your idea about recursion. But I think in Fibonacci, F(1) = F(2) = 1. : )

    ReplyDelete