I wrote a factorial/permutations/combinations program in Java, and I want to do the same in C#. I noticed (from various research) that when you use the ulong variable type, the max value is higher than Java's long. My limitation in the Java version was that the long type was too small to do numbers like 30, or 100. I wrote a little thing that would test if my hypothesis that C# could stretch the range of my program, but I was proven wrong. My Java version can reach approximately 24, but the C# goes to roughly 20. Is there anything I can do to reach my goal?

using System;

namespace codeAdmiral{
  public class Factorial{
     public static void Main(){
        ulong num;
        Console.WriteLine("Enter value");
        num = ulong.Parse(Console.ReadLine());
        ulong factorial = 1;
        for (ulong forBlockvar = num; forBlockvar > 1; forBlockvar--){
                 factorial *= forBlockvar;
        Console.WriteLine("The Factorial for the Given input is:");
  • 2
    \$\begingroup\$ Use double if you want a greater range. \$\endgroup\$ May 30, 2012 at 15:06
  • \$\begingroup\$ In Java, things got screwed up when i tried to use a double... Are ulong and double interchangable like int and long in Java?? \$\endgroup\$ May 30, 2012 at 15:08
  • 1
    \$\begingroup\$ @JesseC.Slicerm Using double to compute precise values is not a good idea. \$\endgroup\$
    – svick
    May 30, 2012 at 15:31
  • \$\begingroup\$ Not in the least. Check out these tables for what their differences are: msdn.microsoft.com/en-us/library/exx3b86w and msdn.microsoft.com/en-us/library/9ahet949.aspx \$\endgroup\$ May 30, 2012 at 15:33
  • 3
    \$\begingroup\$ @JesseC.Slicer Are you sure? \$\endgroup\$
    – svick
    May 30, 2012 at 15:36

1 Answer 1


Signed 64-bit integer (long in both Java and C#) can have values between approximately -9 · 1018 and 9 · 1018. Unsigned 64-bit integer (ulong in C#) can have values between 0 and approximately 1.8 · 1019. The value of 20! is approximately 2 · 1018, so it fits comfortably both to long and ulong. The value of 21! is approximately 5 · 1019, so it doesn't fit even into ulong.

Because of this, 20! should be computed correctly both in Java and c#, but 21! should be incorrect in both, no matter whether you use long or ulong.

If you want to make sure you are getting correct results in C#, you can use unchecked. If you do that and the computation overflows, you will get an exception.

What should you do if you want to compute larger values than 20!? The best option would be if there was some type that could represent arbitrarily large integers. Fortunately, both languages have them, there is BigInteger in C# and BigInteger in Java.

  • \$\begingroup\$ Alright, thanks!! I have tried to use BigInteger in the Java version a while back, but that was before I understood oo programming... \$\endgroup\$ May 30, 2012 at 16:22
  • 1
    \$\begingroup\$ The fact that factorial grows so fast makes a compelling case to store the first 20 results pre-computed in a static look-up array for practical implementation. \$\endgroup\$
    – Leonid
    May 30, 2012 at 18:11
  • \$\begingroup\$ But that just isnt the same :) \$\endgroup\$ May 30, 2012 at 18:19
  • 1
    \$\begingroup\$ @Leonid I don't see much reason to do that. Computing 20! is so fast that pre-computing it most likely won't speed up your code noticeably. \$\endgroup\$
    – svick
    May 30, 2012 at 18:44
  • \$\begingroup\$ And it's more satisfiying to write something that computes it rather than just creating an array :) \$\endgroup\$ Jun 1, 2012 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.