Click here to Skip to main content
15,878,809 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Calculate the Factorial of an Integer in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
18 Oct 2011CPOL 22K   1   12
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...
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.

C#
static uint Factorial(uint x)
{
  if (x > 12)
    throw new ArgumentException("Cannot calculate a factorial for numbers larger than 12");
  return _factorials[x];
}
static readonly uint[] _factorials = new uint[13] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600 };


Edit 10/11/2011: Corrected for goof on the last number (thanks Graham Toal).

License

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


Written By
Architect
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions

 
GeneralRe: I used an array of ints of 5 digit length as the product wou... Pin
Matthew Dennis18-Oct-11 13:55
sysadminMatthew Dennis18-Oct-11 13:55 
GeneralRe: Did you just create your own data structure and perform the ... Pin
Andrew Rissing18-Oct-11 13:46
Andrew Rissing18-Oct-11 13:46 
GeneralI hope you don't mind that I modified your tip. Unless I'm m... Pin
Qwertie18-Oct-11 8:02
Qwertie18-Oct-11 8:02 
GeneralRe: I knew it would. I just didn't want to change the context o... Pin
Andrew Rissing18-Oct-11 13:42
Andrew Rissing18-Oct-11 13:42 
GeneralReason for my vote of 5 I like the idea a switch case can do... Pin
__erfan__17-Oct-11 22:02
__erfan__17-Oct-11 22:02 
GeneralReason for my vote of 5 It's fast and simple. Pin
zatuliveter17-Oct-11 19:57
zatuliveter17-Oct-11 19:57 
GeneralReason for my vote of 4 4 for the smartest solution; 1 point... Pin
Graham Toal11-Oct-11 10:00
Graham Toal11-Oct-11 10:00 
GeneralRe: Corrected above. Thanks for the catch. Pin
Andrew Rissing11-Oct-11 10:19
Andrew Rissing11-Oct-11 10:19 
GeneralHow can an number ending on 00 suddenly end in 504 when mult... Pin
Graham Toal11-Oct-11 9:58
Graham Toal11-Oct-11 9:58 
GeneralReason for my vote of 5 That's the true way to do it. So man... Pin
YvesDaoust10-Oct-11 20:40
YvesDaoust10-Oct-11 20:40 
GeneralRe: I had an assignment in university to calculate 72!, all 100 ... Pin
Matthew Dennis18-Oct-11 13:30
sysadminMatthew Dennis18-Oct-11 13:30 
GeneralReason for my vote of 5 Best way to dodge a buffer overflow ... Pin
tamcntt19854-Oct-11 15:50
tamcntt19854-Oct-11 15:50 

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.