Greatest Common Divisor from a set of more than 2 integers

后端 未结 13 1064
遇见更好的自我
遇见更好的自我 2020-12-09 04:25

There are several questions on Stack Overflow discussing how to find the Greatest Common Divisor of two values. One good answer shows a neat recursive function

13条回答
  •  被撕碎了的回忆
    2020-12-09 05:15

    Rewriting this as a single function...

        static int GCD(params int[] numbers)
        {
            Func gcd = null;
            gcd = (a, b) => (b == 0 ? a : gcd(b, a % b));
            return numbers.Aggregate(gcd);
        } 
    

提交回复
热议问题