GCD Simplified using lambda





4.00/5 (3 votes)
Get GCD
Here is a trick which simplifies GCD using Lambda expression:
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
.