Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
Reduce indentation level. Give syntax hint. Nice name for SO link.
Source Link
user40980
user40980

http://stackoverflowWhat is the maximum possible length of a .com/questions/140468/what-is-the-maximum-possible-length-of-a-net-stringNET string?

 /// <summary> /// Adds two "integers". The integers can be of any size string. /// </summary> /// <param name="BigInt1">The first integer</param> /// <param name="BigInt2">The second integer</param> /// <returns>A string that is the addition of the two integers passed.</returns> /// <exception cref="Exception">Can throw an exception when parsing the individual parts of the number. Callers should handle. </exception> public string AddBigInts(string BigInt1, string BigInt2) { string result = string.Empty; //Make the strings the same length, pre-pad the shorter one with zeros int length = (BigInt1.Length > BigInt2.Length ? BigInt1.Length : BigInt2.Length); BigInt1 = BigInt1.PadLeft(length, '0'); BigInt2 = BigInt2.PadLeft(length, '0'); int remainder = 0; //Now add them up going from right to left for (int i = (BigInt1.Length - 1); i >= 0; i--) { //If we don't encounter a number, this will throw an exception as indicated. int int1 = int.Parse(BigInt1[i].ToString()); int int2 = int.Parse(BigInt2[i].ToString()); //Add int add = int1 + int2 + remainder; //Check to see if we need a remainder; if (add >= 10) { remainder = 1; add = add % 10; } else { remainder = 0; } //Add this to our "number" result = add.ToString() + result; } //Handle when we have a remainder left over at the end if (remainder == 1) { result = remainder + result; } return result; } 
/// <summary> /// Adds two "integers". The integers can be of any size string. /// </summary> /// <param name="BigInt1">The first integer</param> /// <param name="BigInt2">The second integer</param> /// <returns>A string that is the addition of the two integers passed.</returns> /// <exception cref="Exception">Can throw an exception when parsing the individual parts of the number. Callers should handle. </exception> public string AddBigInts(string BigInt1, string BigInt2) { string result = string.Empty; //Make the strings the same length, pre-pad the shorter one with zeros int length = (BigInt1.Length > BigInt2.Length ? BigInt1.Length : BigInt2.Length); BigInt1 = BigInt1.PadLeft(length, '0'); BigInt2 = BigInt2.PadLeft(length, '0'); int remainder = 0; //Now add them up going from right to left for (int i = (BigInt1.Length - 1); i >= 0; i--) { //If we don't encounter a number, this will throw an exception as indicated. int int1 = int.Parse(BigInt1[i].ToString()); int int2 = int.Parse(BigInt2[i].ToString()); //Add int add = int1 + int2 + remainder; //Check to see if we need a remainder; if (add >= 10) { remainder = 1; add = add % 10; } else { remainder = 0; } //Add this to our "number" result = add.ToString() + result; } //Handle when we have a remainder left over at the end if (remainder == 1) { result = remainder + result; } return result; } 

http://stackoverflow.com/questions/140468/what-is-the-maximum-possible-length-of-a-net-string

 /// <summary> /// Adds two "integers". The integers can be of any size string. /// </summary> /// <param name="BigInt1">The first integer</param> /// <param name="BigInt2">The second integer</param> /// <returns>A string that is the addition of the two integers passed.</returns> /// <exception cref="Exception">Can throw an exception when parsing the individual parts of the number. Callers should handle. </exception> public string AddBigInts(string BigInt1, string BigInt2) { string result = string.Empty; //Make the strings the same length, pre-pad the shorter one with zeros int length = (BigInt1.Length > BigInt2.Length ? BigInt1.Length : BigInt2.Length); BigInt1 = BigInt1.PadLeft(length, '0'); BigInt2 = BigInt2.PadLeft(length, '0'); int remainder = 0; //Now add them up going from right to left for (int i = (BigInt1.Length - 1); i >= 0; i--) { //If we don't encounter a number, this will throw an exception as indicated. int int1 = int.Parse(BigInt1[i].ToString()); int int2 = int.Parse(BigInt2[i].ToString()); //Add int add = int1 + int2 + remainder; //Check to see if we need a remainder; if (add >= 10) { remainder = 1; add = add % 10; } else { remainder = 0; } //Add this to our "number" result = add.ToString() + result; } //Handle when we have a remainder left over at the end if (remainder == 1) { result = remainder + result; } return result; } 

What is the maximum possible length of a .NET string?

/// <summary> /// Adds two "integers". The integers can be of any size string. /// </summary> /// <param name="BigInt1">The first integer</param> /// <param name="BigInt2">The second integer</param> /// <returns>A string that is the addition of the two integers passed.</returns> /// <exception cref="Exception">Can throw an exception when parsing the individual parts of the number. Callers should handle. </exception> public string AddBigInts(string BigInt1, string BigInt2) { string result = string.Empty; //Make the strings the same length, pre-pad the shorter one with zeros int length = (BigInt1.Length > BigInt2.Length ? BigInt1.Length : BigInt2.Length); BigInt1 = BigInt1.PadLeft(length, '0'); BigInt2 = BigInt2.PadLeft(length, '0'); int remainder = 0; //Now add them up going from right to left for (int i = (BigInt1.Length - 1); i >= 0; i--) { //If we don't encounter a number, this will throw an exception as indicated. int int1 = int.Parse(BigInt1[i].ToString()); int int2 = int.Parse(BigInt2[i].ToString()); //Add int add = int1 + int2 + remainder; //Check to see if we need a remainder; if (add >= 10) { remainder = 1; add = add % 10; } else { remainder = 0; } //Add this to our "number" result = add.ToString() + result; } //Handle when we have a remainder left over at the end if (remainder == 1) { result = remainder + result; } return result; } 
Source Link
Jon Raynor
  • 11.8k
  • 32
  • 49

When dealing with large numbers, probably one of the most fundamental design decisions is how am I going to represent the large number?

Will it be a string, an array, a list, or custom (homegrown) storage class.

After that decision is made, the actual math operations can be broken down in smaller parts and then executed with native language types such as int or integer.

I've included a very rudimentary ADDITION example in C# .Net that stores the resulting large number as a string. Incoming "numbers" are also strings, so one should be able to send in very "large" numbers. Keep in mind that the example is only for whole numbers to keep it simple.

Even with strings there is a limit in the number of characters or "numbers" in the number, as indicated here:

http://stackoverflow.com/questions/140468/what-is-the-maximum-possible-length-of-a-net-string

But you could add some really big numbers, way beyond int32 or int64 native types for .Net.

Anyway, here's a string storage implementation.

 /// <summary> /// Adds two "integers". The integers can be of any size string. /// </summary> /// <param name="BigInt1">The first integer</param> /// <param name="BigInt2">The second integer</param> /// <returns>A string that is the addition of the two integers passed.</returns> /// <exception cref="Exception">Can throw an exception when parsing the individual parts of the number. Callers should handle. </exception> public string AddBigInts(string BigInt1, string BigInt2) { string result = string.Empty; //Make the strings the same length, pre-pad the shorter one with zeros int length = (BigInt1.Length > BigInt2.Length ? BigInt1.Length : BigInt2.Length); BigInt1 = BigInt1.PadLeft(length, '0'); BigInt2 = BigInt2.PadLeft(length, '0'); int remainder = 0; //Now add them up going from right to left for (int i = (BigInt1.Length - 1); i >= 0; i--) { //If we don't encounter a number, this will throw an exception as indicated. int int1 = int.Parse(BigInt1[i].ToString()); int int2 = int.Parse(BigInt2[i].ToString()); //Add int add = int1 + int2 + remainder; //Check to see if we need a remainder; if (add >= 10) { remainder = 1; add = add % 10; } else { remainder = 0; } //Add this to our "number" result = add.ToString() + result; } //Handle when we have a remainder left over at the end if (remainder == 1) { result = remainder + result; } return result; } 

I hope that gives you some ideas on your own implementation. Please note, that the sample code probably isn't optimized or anything like that. It is meant to give some ideas of how it could be done.