working with incredibly large numbers in .NET
Consider System.Numerics.BigInteger.
how to handle extremely large numbers
Use a BigInteger
The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds.
C#: How Should I Handle Arithmetic with Huge Numbers?
.NET 4 will have this built in via the BigInteger type. This is claimed to be pretty well tuned and should perform very well.
For 3.5 and earlier, you can grab an implementation of BigInteger from the Dynamic Language Runtime sources. (See e.g. http://dlr.codeplex.com/sourcecontrol/changeset/view/40021?projectName=dlr#694008 and drill down into Src / Runtime / Microsoft.Dynamic / Math.) I don't know if this has been tuned as highly as the .NET 4 BigInteger type, but it should still be more efficient than your string version because it internally represents the big numbers using integral types and performs arithmetic using integer operations.
Very large number manipulations in C#
For big floating point numbers you could use BigRational. This answer has a nice explanation about it.
Storing a very, very large number theoretical
The claim that BigInteger
"in theory has no upper or lower bounds" is incorrect. In .NET 4.0, the BigInteger
struct is internally represented using an uint[]
array. Given that the maximum value for uint
is 4,294,967,295, and that the maximum length for the array is 2,146,435,071, the current implementation of BigInteger
has a theoretical upper bound of 4,294,967,295 2,146,435,071 (assuming perfect packing). This allows it to store integers consisting of billions of digits (including your prime), but not trillions.
Edit: As mentioned in the comments, arrays cannot exceed 2 gigabytes in total size, unless the <gcAllowVeryLargeObjects>
setting in enabled (which requires .NET 4.5 and 64-bit). Since the uint
data type occupies 4 bytes, the maximum number of elements in the array is limited to 229.
To demonstrate this upper bound, all you need to do is run the following code, which attempts to calculate (230)(230).
var bi = BigInteger.Pow(1 << 30, 1 << 30);
Within a few seconds, you would get an error:
OutOfMemoryException: Array dimensions exceeded supported range.
Do not be misled by the name of the exception type; this error will be thrown even if you have abundant memory to accommodate the entire number. The same error would, in fact, be thrown if you run the following snippet:
var s = new uint[1 << 30];
Very, very large numbers in vb.net
System.Numerics.BigInteger in .NET 4.0.
Related Topics
Getting All Possible Combinations from a List of Numbers
How Does One Extract Each Folder Name from a Path
Discovering Derived Types Using Reflection
Should I Take Ilogger, Ilogger<T>, Iloggerfactory or Iloggerprovider for a Library
How to Prevent Synchronous Continuations on a Task
Large Wcf Web Service Request Failing with (400) Http Bad Request
Cannot Find .Cs Files for Debugging .Net Source Code
ASP.NET MVC 3: Override "Name" Attribute with Textboxfor
How to Embed Multiple Images in Email Body Using .Net
How to Bind Datatable to Datagridview in C#
Func Delegate with Ref Variable
String Interpolation with Format Variable