C# find the greatest common divisor

后端 未结 9 1519
夕颜
夕颜 2020-12-02 23:11

\"The greatest common divisor of two integers is the largest integer that evenly divides each of the two numbers. Write method Gcd that returns the greatest common divisor o

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 23:45

    You can try using this:

    static int GreatestCommonDivisor(int[] numbers)
    {
        return numbers.Aggregate(GCD);
    }
    
    static int GreatestCommonDivisor(int x, int y)
    {
    return y == 0 ? x : GreatestCommonDivisor(y, x % y);
    }
    

提交回复
热议问题