Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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:");
share|improve this question
Use double if you want a greater range. –  Jesse C. Slicer May 30 '12 at 15:06
In Java, things got screwed up when i tried to use a double... Are ulong and double interchangable like int and long in Java?? –  fr00ty_l00ps May 30 '12 at 15:08
@JesseC.Slicerm Using double to compute precise values is not a good idea. –  svick May 30 '12 at 15:31
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 –  Jesse C. Slicer May 30 '12 at 15:33
@JesseC.Slicer Are you sure? –  svick May 30 '12 at 15:36

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer
Alright, thanks!! I have tried to use BigInteger in the Java version a while back, but that was before I understood oo programming... –  fr00ty_l00ps May 30 '12 at 16:22
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. –  Leonid May 30 '12 at 18:11
But that just isnt the same :) –  fr00ty_l00ps May 30 '12 at 18:19
@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. –  svick May 30 '12 at 18:44
And it's more satisfiying to write something that computes it rather than just creating an array :) –  fr00ty_l00ps Jun 1 '12 at 14:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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