Function for Factorial.

S

Sugandh Jain

Hi.
How to write a function that will return me the factorial (say in a string)
for the any positive integer it takes?

When we find a factorial of even say 2000 or a higher number, it will be
very big to be accomodated in int or long datatype.

Regards,
Sugandh
 
K

Kevin Spencer

You can write a recursive function, which I've illustrated below:

Note, however, that I'm using the largest positive integer data type
(Uint64), and that you can only get values for numbers up to 21 with it.
Anything larger throws an overflow exception, as the result is larger than
UInt64.MaxValue.

To calculate larger numbers, you would need to create your own integral data
type, with storage larger than 64 bits.

/// <summary>
/// Returns the factorial of any UInt64 less than 22
/// </summary>
/// <param name="n">The number to get a factorial for.</param>
/// <returns>The factorial of n.</returns>
/// <remarks>Throws an exception if the number passed is greater than
21</remarks>
public static UInt64 Factorial(UInt64 n)
{
UInt64 i = Factorial(n - 1, 0);
return i;
}

/// <summary>
/// Returns the factorial of any UInt64 less than 22
/// </summary>
/// <param name="n">The number to get a factorial for.</param>
/// <param name="ct">The count of iterations</param>
/// <returns>The factorial of n.</returns>
/// <remarks>Throws an exception if the number passed is greater than
21</remarks>
private static UInt64 Factorial(UInt64 n, int ct)
{
if (n == 0) return 1;
if (n < 0) throw new Exception("Factorials may not be determined for
negative numbers");
if (ct > 21) throw new Exception("Too many recursion levels");
return n * Factorial(n - 1, ct + 1);
}

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
R

Rick Lones

Sugandh said:
Hi.
How to write a function that will return me the factorial (say in a string)
for the any positive integer it takes?

When we find a factorial of even say 2000 or a higher number, it will be
very big to be accomodated in int or long datatype.

Regards,
Sugandh
The simplest way to fake this is to keep an array (of 16 or 32 bit ints) where
each member represents one decimal digit of your running result. To represent
n! you then simulate the multiplication one digit at a time with each new factor
1 through n. At the end each result digit can be converted to a string
representation. Tips: 1) initialize correctly and 2) don't forget the carry!

With 32 bit members you would in principle be good on the multiplication side
for over 200,000,000!; however you may find that you run out of memory for the
digit array somewhat before that . . .

HTH,
-rick-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top