Big integers in C#

C#BigintegerLargenumberJ#

C# Problem Overview


Currently I am borrowing java.math.BigInteger from the J# libraries as described here. Having never used a library for working with large integers before, this seems slow, on the order of 10 times slower, even for ulong length numbers. Does anyone have any better (preferably free) libraries, or is this level of performance normal?

C# Solutions


Solution 1 - C#

As of .NET 4.0 you can use the System.Numerics.BigInteger class. See documentation here: http://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx

Another alternative is the IntX class.

> IntX is an arbitrary precision > integers library written in pure C# > 2.0 with fast - O(N * log N) - multiplication/division algorithms > implementation. It provides all the > basic operations on integers like > addition, multiplication, comparing, > bitwise shifting etc.

Solution 2 - C#

F# also ships with one. You can get it at Microsoft.FSharp.Math.

Solution 3 - C#

The System.Numerics.BigInteger class in .NET 4.0 is based on Microsoft.SolverFoundation.Common.BigInteger from Microsoft Research.

The Solver Foundation's BigInteger class looks very performant. I am not sure about which license it is released under, but you can get it here (download and install Solver Foundation and find the Microsoft.Solver.Foundation.dll).

Solution 4 - C#

I reckon you could optimize the implementation if you perform all the operations on BigInts that are going to return results smaller than a native type (Eg. int64) on the native types and only deal with the big array if you are going to overflow.

edit This implementation on codeproject, seems only 7 times slower ... But with the above optimization you could get it to perform almost identically to native types for small numbers.

Solution 5 - C#

Here are several implementations of BigInteger in C#. I've used Mono's BigInteger implementation, works pretty fast (I've used it in CompactFramework)

Bouncy Castle

Mono

Solution 6 - C#

I'm not sure about the performance, but IronPython also has a BigInteger class. It is in the Microsoft.Scripting.Math namespace.

Solution 7 - C#

Yes, it will be slow, and 10x difference is about what I'd expect. BigInt uses an array to represent an arbitrary length, and all the operations have to be done manually (as opposed to most math which can be done directly with the CPU)

I don't even know if hand-coding it in assembly will give you much of a performance gain over 10x, that's pretty damn close. I'd look for other ways to optimize it--sometimes depending on your math problem there are little tricks you can do to make it quicker.

Solution 8 - C#

I used Biginteger at a previous job. I don't know what kind of performance needs you have. I did not use it in a performance-intensive situation, but never had any problems with it.

Solution 9 - C#

This may sound like a strange suggestion, but have you tested the decimal type to see how fast it works?

The decimal range is ±1.0 × 10^−28 to ±7.9 × 10^28, so it may still not be large enough, but it is larger than a ulong.

There was supposed to be a BigInteger class in .NET 3.5, but it got cut.

Solution 10 - C#

This won't help you, but there was supposed to be a BigInteger class in .Net 3.5; it got cut, but from statements made at PDC, it will be in .Net 4.0. They apparently have spent a lot of time optimizing it, so the performance should be much better than what you're getting now.

Further, this question is essentially a duplicate of https://stackoverflow.com/questions/25375/how-can-i-represent-a-very-large-integer-in-net

Solution 11 - C#

See the answers in this thread. You will need to use one of the third-party big integer libraries/classes available or wait for C# 4.0 which will include a native BigInteger datatype.

Solution 12 - C#

This Looks very promising. It is a C# Wrapper over GMP.

http://web.rememberingemil.org/Projects/GnuMpDotNet/GnuMpDotNet.html

There are also other BigInteger options for .Net here in particular, Mpir.Net

Solution 13 - C#

You can also use the Math.Gmp.Native Nuget package that I wrote. Its source code is available on GitHub, and documentation is available here. It exposes to .NET all of the functionality of the GMP library which is known as a highly-optimized arbitrary-precision arithmetic library.

Arbitrary-precision integer are represented by the mpz_t type. Operations on these integers all begin with the mpz_ prefix. For examples, mpz_add or mpz_cmp. Source code examples are given for each operation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthew ScharleyView Question on Stackoverflow
Solution 1 - C#DavorinView Answer on Stackoverflow
Solution 2 - C#Steve SeveranceView Answer on Stackoverflow
Solution 3 - C#Rasmus FaberView Answer on Stackoverflow
Solution 4 - C#Sam SaffronView Answer on Stackoverflow
Solution 5 - C#Vadym StetsiakView Answer on Stackoverflow
Solution 6 - C#Daniel PlaistedView Answer on Stackoverflow
Solution 7 - C#Bill KView Answer on Stackoverflow
Solution 8 - C#Jason JacksonView Answer on Stackoverflow
Solution 9 - C#PowerlordView Answer on Stackoverflow
Solution 10 - C#technophileView Answer on Stackoverflow
Solution 11 - C#Scott DormanView Answer on Stackoverflow
Solution 12 - C#Charles OkwuagwuView Answer on Stackoverflow
Solution 13 - C#RobertBaronView Answer on Stackoverflow