Click here to Skip to main content
11,634,796 members (64,396 online)
Click here to Skip to main content

Tagged as

Calculate the Factorial of an Integer in C#

, 18 Oct 2011 CPOL 6K 1
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Andrew Rissing
Software Developer (Senior)
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.

You may also be interested in...

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
staffMatthew Dennis18-Oct-11 13:55 
GeneralRe: Did you just create your own data structure and perform the ... Pin
Andrew Rissing18-Oct-11 13:46
memberAndrew 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
memberQwertie18-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
memberAndrew 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
member__erfan__17-Oct-11 22:02 
GeneralReason for my vote of 5 It's fast and simple. Pin
qsmart217-Oct-11 19:57
memberqsmart217-Oct-11 19:57 
GeneralReason for my vote of 4 4 for the smartest solution; 1 point... Pin
Graham Toal11-Oct-11 10:00
memberGraham Toal11-Oct-11 10:00 
GeneralRe: Corrected above. Thanks for the catch. Pin
Andrew Rissing11-Oct-11 10:19
memberAndrew 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
memberGraham 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
memberYvesDaoust10-Oct-11 20:40 
GeneralRe: I had an assignment in university to calculate 72!, all 100 ... Pin
Matthew Dennis18-Oct-11 13:30
staffMatthew Dennis18-Oct-11 13:30 
GeneralReason for my vote of 5 Best way to dodge a buffer overflow ... Pin
tamcntt19854-Oct-11 15:50
membertamcntt19854-Oct-11 15:50 

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.150728.1 | Last Updated 18 Oct 2011
Article Copyright 2011 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid