Alternatives
Members may post updates or alternatives to this current article in order to show different
approaches or add new features.
18 Oct 2011Andrew Rissing
Or you could just go the other direction and cache the known results ahead of time. You're only looking at 13 numbers in all, so it is not a big memory hog to just store those known values inside of the method and be done with it.static uint Factorial(uint x){ if (x > 12) throw...
My preference for run-time speed is:long Factorial(int input){ if (input 1; input--) { if (long.MaxValue - answer < answer) throw new...
Recursion is a neat way of calculating a number's factorial but there is a danger of the stack overflowing when the number is large. The following is a simplified version of the original. It obviates the need for the if else statements within the where loop.int Factorial(int input){ int...
4 Oct 2011Mohammad A Rahman
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...
18 Oct 2011Matthew Dennis
The System.Numeric.BigInteger class allows for calculating VERY LARGE values.I created a sample window form app to calculate the factorial of an input valueprivate void button1_Click(object sender, EventArgs e){ int inputValue; if (int.TryParse(this.textBox1.Text, out inputValue)...
Use Recursion.int factorial(int n){ if(n<0) return -1; if(n==0 || n==1) return 1; return n * factorial(n-1);}