C# - Recursive Euclidean Algorithm (for GCD)

By , 12/22/2011
(1 ratings)
A recursive algorithm used to find the GCD of two numbers.
/// <summary>
/// Finds the GCD of two numbers. (Recursively)
/// </summary>
public int gcd(int a, int b)
{
     if(a == 0)
          return a;
     if(b == 0)
          return b;

     if(a>b)
          return gcd(a % b, b);
     else
          return gcd(a, b % a);
}
Tagged with gcd, euclid, algorithm, recursive.

Comments

 

Log in, to comment!

Related Snippets