Wednesday, February 25, 2015

THE AMAZING RECURSION #Week 5

RECURSION IN DAILY LIFE

It was my first time learning how to use recursion in programming in week #4.  But it was not the first time that I come into contact with recursion. I remembered when I was a child, sometimes I thought that the god (god A) knows my thoughts, and an another god (god B) knows that the god (god A) knows my thoughts, etc.
We can see there are many recursion examples in our daily life. For example:


Therefore, recursion is not only interesting and also is not mysterious.

WHY PROGRAMMERS NEED TO KNOW ABOUT RECURSION

Well, it's said that an ordinary programmer like using iteration, however, a talented programmer like using recursion. Even it is just a joke, but recursion is significant for a programmer. Knowing how to use recursion can let your code run more efficiently, and since most of the programs are a teamwork, therefore, reducing the amount of codes is also important for a good programmer.
Here is the comparison between iteration and recursion:
==========================================
Iteration:
def factorial(number):
product = 1
for i in range(number):
product = product * ( i + 1 )
return product

Recursion:
def factorial(number):
if number <= 1:
return 1
else:
return number * factorial(number - 1)

===========================================
Basically, the code of recursion looks simpler than the code of iteration. Image if you are working with a group and your teammates have to read your code. It's important that keep your code as simple as possible.

WHAT IS RECURSION?

Here is the definition of recursion from WIKIPEDIA
In mathematics and computer science, a class of objects or methods exhibit recursive behavior when they can be defined by two properties:
  1. A simple base case (or cases)
  1. A set of rules that reduce all other cases toward the base case
For example, the following is a recursive definition of a person's ancestors:
  • The ancestors of one's ancestors are also one's ancestors (recursion step).
The Fibonacci sequence is a classic example of recursion:
\text{Fib}(0)=0\text{ as base case 1,}
\text{Fib}(1)=1\text{ as base case 2,}
\text{For all integers }n>1,~\text{ Fib}(n):=\text{Fib}(n-1) + \text{Fib}(n-2).
Many mathematical axioms are based upon recursive rules. For example, the formal definition of the natural numbers by the Peano axioms can be described as: 0 is a natural number, and each natural number has a successor, which is also a natural number. By this base case and recursive rule, one can generate the set of all natural numbers.
Recursively defined mathematical objects include functions, sets, and especially fractals.
There are various more tongue-in-cheek "definitions" of recursion; see recursive humor. 

THE DIFFERENCE BETWEEN RECURSION AND ITERATION

An iterative algorithm is based on a repetition of a set of instructions (block) using a loop construct of the programming language. However, a recursive algorithm repeats the code by calling itself. The main advantage of recursive algorithms is their compactness and understandability. Their disadvantage is a need of additional system resources to maintain the call stack.

CONCLUSION:

Personally, although using recursion can reduce the amount of codes, however, something it might cause a serious bug if we use recursion casually because it would cause recursion depth goes infinity and it will take up the whole memory.

No comments:

Post a Comment