Content deleted Content added
m Reverted edits by 164.58.107.203 (talk) to last version by Jerryobject |
Some basic edits. (The article needs more edits. There are broken citations and could do with some book and article sources. It needs expanded and clarified.) Tag: references removed |
||
Line 1:
In computing, '''recursion termination''' is when certain conditions are met and a [[recursive algorithm]] stops calling itself and begins to return values.
{{Citation
| title =
| url=https://www.youtube.com/watch?v=Mv9NEXX1VHc
}}
</ref>
Line 9:
==Examples==
===Fibonacci
The [[Fibonacci function]](fibonacci(n)), which takes [[integer]] n(n >= 0) as input, has three conditions
1.
2.
3.
This recursive function terminates if either conditions 1 or 2 are satisfied. We see that the function's recursive call reduces the value of n(by passing n-1 or n-2 in the function) ensuring that n reaches either condition 1 or 2.
If we look at an example of this in Python we can see what's going on a bit more clearly:
<pre>
def fibonacci (n):
if n == 1:
return 1
elif n == 2:
return 1
elif n > 2:
return fibonacci(n-1) + fibonacci(n-2)
</pre>
===Factorial Example===
An example in the programming language [[C++]]:<ref>
{{cite book
Line 34 ⟶ 45:
}
</pre>
Here we see that in the recursive call, the number passed in the recursive step is reduced by 1. This again ensures that the number will at some point reduce to 0 which in turn terminates the recursive algorithm since we have our base case where we know that the factorial of 0 is 1 (or 0! = 1).
In the C programing language we could similarly do something like this:
<pre>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
</pre>
==References==
{{reflist
==External links==
* [http://www.cs.princeton.edu/courses/archive/spr05/cos126/lectures/07.pdf Princeton university: "An introduction to computer science in the context of scientific, engineering, and commercial applications"]
* [http://www.cdf.toronto.edu/~csc148h/winter/stg/lectures/w3/1/m/Recursion.pdf University of Toronto: "Introduction to Computer Science"]
* [https://www.youtube.com/watch?v=Mv9NEXX1VHc Computerphile: "What On Earth is Recursion?"]
[[Category:Recursion]]
|