Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C# 4.0
Tip/Trick

GCD Simplified using lambda

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
8 May 2011CPOL 14.3K   2   4
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Social Group (No members)


Comments and Discussions

 
GeneralNot very efficient! Have a look at http://en.wikipedia.org/w... Pin
Richard Deeming11-May-11 5:57
mveRichard Deeming11-May-11 5:57 
Generalok Pin
Steven.Pinto20006-May-11 18:09
Steven.Pinto20006-May-11 18:09 
GeneralCool recursion! May I, however, recommend a better name? Ot... Pin
Chris Trelawny-Ross6-May-11 10:53
Chris Trelawny-Ross6-May-11 10:53 
GeneralRe: Ok thanks Pin
Steven.Pinto20006-May-11 18:11
Steven.Pinto20006-May-11 18:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.