Most of the time there are many ways to implement an algorithm.
Here are two ways to implement the greatest common divisor algoritm, one of the oldest algorithms ever.

128 Views
//recursive method
    //
    public static int gcd(int x, int y)
        {
            if (y == 0) return x;
            else return gcd(y, x % y);  
        }


    //more like the ancient greek Euclid originally devised it
    //
    public static int gcd2(int x, int y)
        {
            while (x != y)
            {
                if (x > y)
                {
                    x = x - y;
                }
                else
                {
                    y = y - x;
                }
            }
         return x;
        }
About the Author

Have programmed in Modula-2 on a professional basis in the eighties. Now I am quite fond of C# AND Python!