Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Calculate the Factorial of an Integer in C#

, 18 Oct 2011
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)

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.

Comments and Discussions

 
GeneralRe: I used an array of ints of 5 digit length as the product wou... PinstaffMatthew Dennis18-Oct-11 13:55 
GeneralRe: Did you just create your own data structure and perform the ... PinmemberAndrew Rissing18-Oct-11 13:46 
GeneralI hope you don't mind that I modified your tip. Unless I'm m... PinmemberQwertie18-Oct-11 8:02 
GeneralRe: I knew it would. I just didn't want to change the context o... PinmemberAndrew Rissing18-Oct-11 13:42 
GeneralReason for my vote of 5 I like the idea a switch case can do... Pinmember__erfan__17-Oct-11 22:02 
GeneralReason for my vote of 5 It's fast and simple. Pinmemberqsmart217-Oct-11 19:57 
GeneralReason for my vote of 4 4 for the smartest solution; 1 point... PinmemberGraham Toal11-Oct-11 10:00 
GeneralRe: Corrected above. Thanks for the catch. PinmemberAndrew Rissing11-Oct-11 10:19 
GeneralHow can an number ending on 00 suddenly end in 504 when mult... PinmemberGraham Toal11-Oct-11 9:58 
GeneralReason for my vote of 5 That's the true way to do it. So man... PinmemberYvesDaoust10-Oct-11 20:40 
GeneralRe: I had an assignment in university to calculate 72!, all 100 ... PinstaffMatthew Dennis18-Oct-11 13:30 
GeneralReason for my vote of 5 Best way to dodge a buffer overflow ... Pinmembertamcntt19854-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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 18 Oct 2011
Article Copyright 2011 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid