C# find the greatest common divisor

后端 未结 9 1544
夕颜
夕颜 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:34

    If efficiency is not a big concern this will do the job.

    // gets greatest common divisor of A and B. 
    var GCD=Enumerable.Range(1,Math.Min(A,B)).Last(n=>(A%n | B%n)==0);
    

提交回复
热议问题