Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Function for Factorial.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sugandh Jain - 30 Apr 2007 13:11 GMT
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
Kevin Spencer - 30 Apr 2007 19:04 GMT
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);
 }

Signature

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

> Hi.
> How to write a function that will return me the factorial (say in a
[quoted text clipped - 5 lines]
> Regards,
> Sugandh
Rick Lones - 01 May 2007 00:07 GMT
> Hi.
> How to write a function that will return me the factorial (say in a string)
[quoted text clipped - 5 lines]
> 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-
Arne Vajhøj - 09 Jun 2007 23:56 GMT
> 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.

You will need a biginteger class.

Google finds:
  http://www.codeproject.com/csharp/biginteger.asp

Arne

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.