Recursion (computer science): Difference between revisions

Content deleted Content added
Line 40:
'''end'''
 
Another comparison that even more clearly demonstrates the relative "elegance" of recursive functions is the [[Euclidean Algorithm]], used to compute the [[greatest common factor]] of two integers. Below is the algorithm with recursion, coded in [[C++]]:
 
int GCF(int x, int y)
{
int r = x % y;
if (r == 0)
return y;
else
return GCF(y, r);
}
 
Line 57:
int GCF(int x, int y)
{
int r;
do
{
r = x % y;
x = y;
y = r;
} while (r != 0);
return x;
}