logo.Gif

Squak Mountain Consulting


BigAdd:    Adds two numbers that are up to 2,147,483,647 digits in length.

      public string BigAdd( 
         string a1, 
         string a2 )
      {
         // Adds two numbers represented as character strings.
         // The numbers and their result are limited to 2,147,483,647 digits in length.
         // This code currently handle positive numbers only.
         // This code is intended to run in the Microsoft(r) .NET evironment.
         // Version .03 - Convert tabs to spaces, so that HTML pages read ok
         // Version .02 - Note positive numbers only
         // Version .01 - Original
         //
         // COPYRIGHT (C) 2002 - BOB MEIZLIK AND SQUAK MOUNTAIN CONSULTING ( THE AUTHOR(S) ).
         //
         // NO WARRANTY IS STATED EITHER EXPRESS OR IMPLIED.
         // REDISTRIBUTION MUST RETAIN THIS ABOVE COPYRIGHT NOTICE IN ITS ENTIRETY.
         // THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
         // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
         // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
         // DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY 
         // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
         // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
         // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
         // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
         // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
         // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
         // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
         // PLAIN LANGUAGE: IF YOU USE THIS AND IT CAUSES SOME HARM, IT'S YOUR FAULT.
         // ALL RIGHTS RESERVED.
         // REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS, 
         // WITH OR WITHOUT MODIFICATION, 
         // ARE PERMITTED PROVIDED THAT:
         // 1. THE USER MUST EMAIL 
         //    THE AUTHOR(S) AT INFO@SQUAKMT.COM, SUCH EMAIL MUST INCLUDE:
         //    1a. A VALID RETURN EMAIL ADDRESS
         //    1b. A COPY OF (OR PROPERLY OPERATING LINK TO A COPY OF) THE RESULTANT SOFTWARE
         //    1c. A LICENSE GRANTING THE AUTHOR(S) FREE USE FOR THE SOFWARE'S INTENDED PURPOSE
         // 2. THE USER MUST INCLUDE THIS COPYRIGHT NOTICE IN BOTH DOCUMENTATION AND SOURCE CODE
         //    OF ANY RESULTING SOFTWARE.
         //
   

         int opLen         // Number of digits to operate on in each pass...
          = decimal.MaxValue.ToString().Length  - 2;
                          // minus 1 for sign, and 1 for overflow
         int newLen = 0;   // Length of operands after padding
         int maxLen = 0;   // Length of biggest operand before padding
         int remLen = 0;   // Diff in length between biggest operand
                          // and next opLen boundary
         int opStart=0;    // char position to start operating on

         decimal opTemp =  0; // intermediate results
         string  opChar = ""; // intermediate results
         decimal carry  =  0; // carry out to next result
         string  result = ""; // final result

         // Get the longest number's length
         maxLen = Math.Max( a1.Length , a2.Length );

         // get the difference between that and the next opLen boundary
         remLen = maxLen % opLen;

         // The length of each number is the next higher opLen boundary
         newLen = maxLen + ( opLen - remLen ) ;

         // Pad the numbers to their new lengths
         a1 = a1.PadLeft( newLen, '0' );
         a2 = a2.PadLeft( newLen, '0' );

         // Loop thru the number strings, in opLen-sized chunks
         for( opStart = newLen - opLen
            ; opStart > -1
            ; opStart = opStart - opLen )
         {
            opTemp // add the chunks, and any prior carry
               = decimal.Parse( a1.Substring( opStart, opLen ) )
               + decimal.Parse( a2.Substring( opStart, opLen ) )
               + carry
               ; 

            // Convert the addition results to a string
            opChar = opTemp.ToString();

            // Pad the string to opLen + carry length
            opChar = opChar.PadLeft( opLen + 1, '0');

            // Extract the carry
            carry  = decimal.Parse( opChar.Substring(0, 1) );

            // Extract the non-carry portion
            opChar = opChar.Substring( 1, opLen );

            // Append it to the result
            result = opChar + result;
         }

         // Add any remaining carry to the result
         result = carry.ToString() + result;

         // Return the result
         return result;
      }

At Squak Mountain Consulting we create computer software.

We specialize in implementing the full life cycle of 3 to 12 month projects.

Your project can be completed on-site in the Puget Sound area, or via the internet.

We are versed in a wide variety of current and legacy technologies.

Click here to send us an email with your area of interest and contact information, and get your project DONE !

Our staff is ICCP and Microsoft MCSD certified.