Recursion - Such a mysterious and powerful tool !
- Recursion
At the first sight of the word 'Recursion', I thought it is the same as 'Loop' or 'Iteration'. But, actually, it owns the very different principle.
Before I learn recursion, I might do a math of computing 1 + 2 + 3 + .. .+ 100 with a loop like:
sum = 0
for i in range(101):
sum += i
After I learn recursion , I will do like this:
def gs(x):
if x != 0:
return x + gs(x-1)
else:
return 0
To my mind, I think the essence of recursion is to allocate a task into several small pieces with the same operation method. Because, the best thing that a computer to do is to repeat a thing for thousands of times.
Hence, recursion just takes this advantage to solve some very heavy but actually simple and repeated work.However, the most important and difficult thing is to analyse a task and could think of that it could be allocate into many small simple tasks.And then, write down their relationships by codes, which is such a challenge and needs many tons of practice.
In a word, more you practice, more you learn.
Hence, recursion just takes this advantage to solve some very heavy but actually simple and repeated work.However, the most important and difficult thing is to analyse a task and could think of that it could be allocate into many small simple tasks.And then, write down their relationships by codes, which is such a challenge and needs many tons of practice.
In a word, more you practice, more you learn.