Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

GCD Simplified using lambda

4.00/5 (3 votes)
8 May 2011CPOL 15.4K  
Get GCD
Here is a trick which simplifies GCD using Lambda expression:

C#
Func<int,int,int> GCDrecussion = null;
              GCDrecussion = (z,x) => x == 0 ? z : GCDrecussion(x, z % x);
             Console.WriteLine(GCDrecussion(4, 6));


The Function GCDrecussion takes 2 parameters; both are of int type, in my case I have passed 4 and 6 and the function returns int.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)