Value is greater than ULong? Calculation 100!

I am trying to calculate 100! and there seems to be no built-in factorial function. So I wrote:

Protected Sub ComputeFactorial(ByVal n As ULong) Dim factorial As ULong = 1 Dim i As Integer For i = 1 To n factorial = factorial * i Next lblAnswer.Text = factorial End Sub 

Unfortunately running this value with a value of 100 for n rseults in

The value was either too large or too small for UInt64.

So, is there a larger data type for storing numbers? Am I mistaken in my methods? Am i helpless?

+4
source share
5 answers

Sounds like Project Euler.

.NET 4.0 has System.Numerics.BigInteger, or you can get a pretty sweet implementation here:
C # BigInteger Class

Edit: treed: (

I will add - the version in CodeProject has additional functions, such as integer square root, primality test, Lucas sequence generation. Also, you don't have direct buffer access in the .NET implementation, which was annoying for a couple of things I tried.

+7
source

Until you can use System.Numerics.BigInteger , you'll be stuck using a non-Microsoft implementation, such as BigInteger in your code project .

+1
source

Tip: Use an array to store the digits of a number. You can verify by checking that the result will not contain more than 200 digits.

+1
source

You need an implementation of "BigNums". These are integers that dynamically allocate memory so that they can hold their value.

In fact, the version is cut from BCL .

The J # library has an implementation of java.math.BigInteger , which you can use from any language.

Alternatively, if precision / accuracy is not a problem (you only care about the order of magnitude), you can simply use 64-bit floats.

0
source

decimal will process from 0 to +/- 79,228,162,514,264,337,593,543,950,335 without a decimal point (zero scale)

0
source

All Articles