Click here to Skip to main content
11,487,979 members (79,114 online)
Click here to Skip to main content

Tagged as

Calculate the Factorial of an Integer in C#

, 4 Oct 2011 CPOL 16.6K 2
Rate this:
Please Sign up or sign in to vote.
It could be done using Recursive Lambda Expression, for example,class Program{ static Func Factorial = x => x < 0 ? -1 : x == 1 || x == 0 ? 1 : x * Factorial(x - 1); static void Main(string[] args) { Console.WriteLine(Factorial(5)); }}(The code...
It could be done using Recursive Lambda Expression, for example,

class Program
{
    static Func<int, int> Factorial = x => x < 0 ? -1 : x == 1 || x == 0 ? 1 : x * Factorial(x - 1);
 
    static void Main(string[] args)
    {
        Console.WriteLine(Factorial(5));
    }
}
(The code is based on the References sites.)

From the above code, Factorial is variable of type Func<int,int> which is holding a anonymous method. So following line of code...
 x => x < 0 ? -1 : x == 1 || x == 0 ? 1 : x * Factorial(x - 1)
...is the anonymous method and is equivalent to:
int Factorial( int x ){
  if( x<0){
     return -1;
  }else if( x==1 || x==0 )
  {
     return 1;
  }else{
     return x* Factorial(x-1);
  }
}

It is also possible to generate a Factorial for a number without recursion, for example, the following code block will calculate without the recursive method call:

private static int Factorial(int i)
{
    int result = i < 0 ? -1 : i == 0 || i == 1 ? 1 : 1;
    if (i > 0)
        Enumerable.Range(1, i).ToList<int>().ForEach(element => result = result * element);
    return result;
}

and also using Aggregate function of Linq:

private static int Factorial(int i)
{
    return i < 0 ? -1 : i == 0 || i == 1 ? 1 : Enumerable.Range(1, i).Aggregate((counter, value) => counter * value);
}
(initial idea to use Aggregate function found in here[^].)

References

[1] http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx[^]
[2] http://en.wikipedia.org/wiki/Recursion[^]
[3] Factorial Simplified using lambda[^]
[4] http://stackoverflow.com/questions/1426715/factorial-of-n-numbers-using-c-lambda[^]
[5] http://blogs.msdn.com/b/madst/archive/2007/05/11/recursive-lambda-expressions.aspx[^]
Smile | :)

License

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

Share

About the Author


Comments and Discussions

 
GeneralReason for my vote of 1 advocating recursion and barely read... Pin
dmjm-h26-Oct-11 7:30
memberdmjm-h26-Oct-11 7:30 
GeneralI gave it 5, my reason: mind = blown. The first algorithm wa... Pin
KenBonny17-Oct-11 22:35
memberKenBonny17-Oct-11 22:35 
GeneralRe: Thank you very much. :) Pin
Mohammad A Rahman17-Oct-11 23:26
memberMohammad A Rahman17-Oct-11 23:26 
GeneralReason for my vote of 5 Reason for My vote of 5 I liked it.. Pin
RaviRanjankr12-Oct-11 12:03
mvpRaviRanjankr12-Oct-11 12:03 
GeneralRe: Thank you very much Ravi :) Pin
Mohammad A Rahman12-Oct-11 12:54
memberMohammad A Rahman12-Oct-11 12:54 
GeneralNooooooooooo! Please, no more recursive functions for factor... Pin
Chris Maunder4-Oct-11 3:43
adminChris Maunder4-Oct-11 3:43 
Generalnice Pin
kiran dangar4-Oct-11 1:31
memberkiran dangar4-Oct-11 1:31 
GeneralRe: Thanks. Pin
Mohammad A Rahman4-Oct-11 1:37
memberMohammad A Rahman4-Oct-11 1:37 
GeneralGood solution :) I would be interested to see someone do spe... Pin
Edward Nutting3-Oct-11 21:28
memberEdward Nutting3-Oct-11 21:28 
GeneralRe: Thanks Edward :) Pin
Mohammad A Rahman3-Oct-11 23:09
memberMohammad A Rahman3-Oct-11 23:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 4 Oct 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid