Click here to Skip to main content
Click here to Skip to main content

Calculate the Factorial of an Integer in C#

By , 18 Oct 2011
 
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
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: I used an array of ints of 5 digit length as the product wou...staffMatthew Dennis18 Oct '11 - 13:55 
I used an array of ints of 5 digit length as the product would be 10 digits or less, the max on the machine we were using. Then iteratively multiplied the array by the each number and 'normalized' the array items to 5 digits. See my alternate below on using the BigInteger class.
GeneralRe: Did you just create your own data structure and perform the ...memberAndrew Rissing18 Oct '11 - 13:46 
Did you just create your own data structure and perform the calculations using the "by hand" algorithm?
GeneralI hope you don't mind that I modified your tip. Unless I'm m...memberQwertie18 Oct '11 - 8:02 
I hope you don't mind that I modified your tip. Unless I'm mistaken, your original code which used 'new' inside the method, will create a new array on every call, which is not what I'd call "caching" the results.
GeneralRe: I knew it would. I just didn't want to change the context o...memberAndrew Rissing18 Oct '11 - 13:42 
I knew it would. I just didn't want to change the context of the original solution. Either way, not a big deal.
GeneralReason for my vote of 5 I like the idea a switch case can do...member__erfan__17 Oct '11 - 22:02 
Reason for my vote of 5
I like the idea
a switch case can do the same.
GeneralReason for my vote of 5 It's fast and simple.memberqsmart217 Oct '11 - 19:57 
Reason for my vote of 5
It's fast and simple.
GeneralReason for my vote of 4 4 for the smartest solution; 1 point...memberGraham Toal11 Oct '11 - 10:00 
Reason for my vote of 4
4 for the smartest solution; 1 point short for the error in precomputing the factorial constants. (which now being corrected, I've put back up to 5 Smile | :) )
GeneralRe: Corrected above. Thanks for the catch.memberAndrew Rissing11 Oct '11 - 10:19 
Corrected above. Thanks for the catch.
GeneralHow can an number ending on 00 suddenly end in 504 when mult...memberGraham Toal11 Oct '11 - 9:58 
How can an number ending on 00 suddenly end in 504 when multiplied by 13?! (the last item in the array above)
 
Your program to precompute the factorials didn't check for overflow properly!
GeneralReason for my vote of 5 That's the true way to do it. So man...memberYvesDaoust10 Oct '11 - 20:40 
Reason for my vote of 5
That's the true way to do it. So many people overlook the fact that the factorial quickly overflows !

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 18 Oct 2011
Article Copyright 2011 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid