Linascts
Linascts

Reputation: 159

GCD of three numbers (without using array) euclidean algorithm

I have a task, that gives me a little headache here. The goal is to find Greatest Common Divisor with three integers, I succeded in doing it with two fairly easily, but with three it get's a little complicated when I can't use any arrays.

Here is the full code I used, finding gcd from two integers, tests all green:

public static int GetGcdByEuclidean(int a, int b)
        {
            if (a == 0 && b == 0)
            {
                throw new ArgumentException(null);
            }
            else if (a == int.MinValue)
            {
                throw new ArgumentOutOfRangeException(nameof(a));
            }
            else if (b == int.MinValue)
            {
                throw new ArgumentOutOfRangeException(nameof(b));
            }
            else
            {
                int abs1 = Math.Abs(a);
                int abs2 = Math.Abs(b);
                a = abs1;
                b = abs2;

                while (a != 0 && b != 0)
                {
                    if (a > b)
                    {
                        a %= b;
                    }
                    else
                    {
                        b %= a;
                    }
                }

                return a | b;
            }
        }

And now I used the same principle for the GCD by three, but used something I found on web: gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b)..

public static int GetGcdByEuclidean(int a, int b, int c)
        {
            int result = 0;

            if ((a == 0 && b == 0) && c == 0)
            {
                throw new ArgumentException(null);
            }
            else if (a == int.MinValue)
            {
                throw new ArgumentOutOfRangeException(nameof(a));
            }
            else if (b == int.MinValue)
            {
                throw new ArgumentOutOfRangeException(nameof(b));
            }
            else if (c == int.MinValue)
            {
                throw new ArgumentOutOfRangeException(nameof(c));
            }
            else
            {
                int abs1 = Math.Abs(a);
                int abs2 = Math.Abs(b);
                int abs3 = Math.Abs(c);
                a = abs1;
                b = abs2;
                c = abs3;

                while (a != 0 && b != 0 && c != 0)
                {
                    if (a > b && a > c && b > c)
                    {
                        b %= c;
                        a %= b;
                        result = a;
                    }
                    else if (a > b && a > c && b < c)
                    {
                        c %= b;
                        a %= c;
                        result = a;
                    }
                    else if (b > a && b > c && a > c)
                    {
                        a %= c;
                        b %= a;
                        result = b;
                    }
                    else if (b > a && b > c && a < c)
                    {
                        c %= a;
                        b %= c;
                        result = b;
                    }
                    else if (c > a && c > b && a > b)
                    {
                        a %= b;
                        c %= a;
                        result = c;
                    }
                    else
                    {
                        b %= a;
                        c %= b;
                        result = c;
                    }
                }

                return result;
            }
        }

Upvotes: 0

Views: 700

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112392

Just keep your solution for 2 numbers and call it from the one for three numbers by using this formula: gcd(a, b, c) = gcd(a, gcd(b, c))

public static int GetGcdByEuclidean(int a, int b)
{
    // Your working solution for two numbers...
}

public static int GetGcdByEuclidean(int a, int b, int c)
{
    return GetGcdByEuclidean(a, GetGcdByEuclidean(b, c)); 
}

Note, in C# you can have several methods with the same name, as long as the parameter lists are different. The names of the parameters do not matter, only the number or types of the parameters must be different. This is called overloading.


A solution with an arbitrary number of numbers (but at least two):

public static int GetGcdByEuclidean(int a, int b, params int[] other)
{
    int gcd = GetGcdByEuclidean(a, b);
    foreach (int x in other) {
        gcd = GetGcdByEuclidean(gcd, x);
    }
    return gcd; 
}

Upvotes: 2

Related Questions