Code Kata: Large integer comparison size & large integer arithmetic---plus subtraction javascript implementation

Source: Internet
Author: User

The arithmetic of large integers has been a commonplace problem. Many libraries already contain a wide variety of solutions.

As an exercise, we start with the simplest addition and subtraction.

The core idea of addition and subtraction is to use a reverse array to simulate a large number, and then use the two large number of vertical operations.

Addition function :

    • The subtraction function (given after the subtraction function) for the addition Si Cho of the different symbols
    • The symbol is first determined by adding the same symbol
    • Because the input output is a string, you need to remove the 0 from the beginning of the string
1 functionAdd (A, b) {/*Enter a large number of two string types*/2 3     if(A.indexof ('-') >= 0 && b.indexof ('-') < 0){4 5         returnminus (b,a);6     }7     Else if(A.indexof ('-') < 0 && B.indexof ('-') >= 0){8 9         returnMinus (A, b);Ten     } One  A     varSign = ""; -  -     if(A.indexof ('-') >= 0 && b.indexof ('-') >= 0) {/*two negative numbers to add, specify a symbol*/ the  -Sign = "-"; -  -A = A.SUBSTR (1); +  -b = B.substr (1); +     } A  at     varAarr = A.replace (/^0+/, "). Split ('). reverse (); -  -     varBARR = B.replace (/^0+/, "). Split ("). reverse ();/*using reverse array storage*/ -  -     varcarry = 0;/*Rounding Value*/ -  in     varSumarr = []; -  to     varLen = Math.max (Aarr.length, barr.length);/*gets the number of digits of the larger number of digits*/ +  -      for(vari=0;i<=len-1;i++){ the  *         varDigA = parseint (Aarr[i])? parseint (Aarr[i]): 0; $ Panax Notoginseng         varDIGB = parseint (Barr[i])? parseint (Barr[i]): 0; -  the         varDigtotal = DigA + DIGB +carry; +  A         if(i = = len-1) {/*exclude ' 012 ' + ' 012 ' situations like this*/ the  +             if(Digtotal > 0){ -  $ Sumarr.unshift (digtotal); $             } -  -              Break; the         } - Wuyicarry = number (Digtotal >= 10); the  -Digtotal = digtotal% 10; Wu  - Sumarr.unshift (digtotal); About  $     } -  -     returnSign + sumarr.join (' '); -}

When writing subtraction, we find that we need to compare the size first, so we need a large number to compare the size of the function

Compare small functions:

    • Different symbol comparison size, positive number greater than negative
    • Positive comparison size, first comparison length, large value of length
    • The positive length is the same, starting from the highest bit by bit, only to the larger party, the value is greater
    • Negative comparison size, method with positive number, the result can be reversed
    • Because the input output is a string, you need to remove the 0 from the beginning of the string
1 functionCompare (A, b) {2 3     varSign = 1;4 5     if(A.indexof ('-') >= 0 && b.indexof ('-') < 0) {/*comparison of different symbols*/6 7         return-1;8     }9     Else if(A.indexof ('-') < 0 && B.indexof ('-') >= 0) {/*comparison of different symbols*/Ten  One         return1; A     } -     Else if(A.indexof ('-') >= 0 && b.indexof ('-') >= 0) {/*same as negative, specify negation, and change to positive comparison mode*/ -  theSign =-1; -  -A = A.SUBSTR (1); -  +b = B.substr (1); -     } +  AA = A.replace (/^0+/, "); at  -b = B.replace (/^0+/, "); -  -     varFlag; -  -     if(A.length < B.length) {/*Comparison length*/ in  -Flag =-1; to     } +     Else if(A.length >b.length) { -  theFlag = 1; *     } $     Else{Panax Notoginseng  -Flag = 0; the     } +  A     if(flag = = 0) {/*bitwise comparison of the same length*/ the  +         varAarr = A.split ("); -  $         varBARR = B.split ("); $  -          for(vari=0;i<=aarr.length;i++){ -  the             if(Aarr[i] >Barr[i]) { - WuyiFlag = 1; the  -                  Break; Wu             } -             Else if(Aarr[i] >Barr[i]) { About  $Flag =-1; -  -                  Break; -             } A         } +     } the  -     returnSign *Flag; $}

Subtraction function:

    • The addition function of the Si cho for the subtraction of different symbols
    • The same symbol subtraction requires the size to be determined first
    • Because the input output is a string, you need to remove the 0 from the beginning of the string
1 functionMinus (A, b) {2 3     if(A.indexof ('-') >= 0 && b.indexof ('-') < 0){4 5         returnAdd (A, "-" +b);6     }7     Else if(A.indexof ('-') < 0 && B.indexof ('-') >= 0){8 9A = A.SUBSTR (1);Ten  One         returnAdd (A, b); A     } -  -     varSign = ""; the  -     if(Compare (A, B) < 0){ -  -         vartemp =b; +  -b =A; +  AA =temp; at  -Sign = "-"; -     } -  -     varAarr = A.replace (/^0+/, "). Split ('). reverse (); -  in     varBARR = B.replace (/^0+/, "). Split ("). reverse ();/*using reverse array storage*/ -  to     varborrow = 0;/*Borrow Value*/ +  -     varMinusarr = []; the  *     varLen = Math.max (Aarr.length, barr.length);/*gets the number of digits of the larger number of digits*/ $ Panax Notoginseng      for(vari=0;i<=len-1;i++){ -  the         varDigA = parseint (Aarr[i])? parseint (Aarr[i]): 0; +  A         varDIGB = parseint (Barr[i])? parseint (Barr[i]): 0; the  +         varDigminus; -  $         if(i = = len-1){ $  -             if(Diga-borrow <= DIGB) {/*The highest bit is not enough minus the direct jump out of the loop*/ -  the                  Break; -             }Wuyi         } the  -         if(Diga-digb-borrow >= 0){ Wu  -Digminus = DIGA-DIGB-Borrow; About  $}Else{ -  -Digminus = DigA + 10-DIGB-Borrow; -  ABorrow = 1; +         } the  - Minusarr.unshift (digminus); $  the     } the  the     returnSign + minusarr.join (' '); the}

The above given is a signed large integer addition and subtraction of the basic implementation, but the efficiency is not particularly high.

On the net also has through the 10000 binary optimization vertical algorithm, as well as through the bit operation realization Arithmetic method, everybody also may search to see, today's practice is here, next week will give Chuyang division the basic realization.

If you like my article, you can scan the QR code to follow my public number.

Try to share a little bit of my own development and practice experience every day ~
?

Code Kata: Large integer comparison size & large integer arithmetic---plus subtraction javascript implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.