Java implementation of the update impairment technique

Source: Internet
Author: User
Tags greatest common divisor

Let's start by introducing the source of the noun:

Excerpt from Baidu Encyclopedia:

"Nine chapters Arithmetic" is the ancient Chinese mathematics monograph, in which "more subtractive loss technique" can be used to beg two number of greatest common divisor, the original text is: can half the half of, not half, the sub-denominator, the number of children, in order to reduce more, more subtractive loss, and so on. By the number of equal. Vernacular translation: (If you need to numerator the score, then) can binary words, binary (that is, with the numerator). If it is not possible to binary, then compare the denominator and the size of the molecule, with the large number minus the decimal, minus each other, until the difference is equal to the meiosis, with this equal number to numerator. Use stepsEditThe first step: arbitrarily given two positive integers, judging whether they are even. If so, use a 2 reduction, or the second step if not. The second step: reduce the smaller number by a larger number, and then compare the resulting difference with the smaller number, and subtract the decimal number by the large numbers. Continue this operation until the resulting meiosis and difference are equal. The product of several 2 and second intermediate numbers in the first step is the desired greatest common divisor. The "equal number", which is said, is greatest common divisor. The method of seeking "equal number" is "more subtractive". Examples of Use: Example 1, the use of more subtractive loss of 98 and 63 greatest common divisor. Solution: Since 63 is not even, the number of 98 and 63 is reduced by a large number and subtracted: 98-63=3563-35=2835-28=728-7=2121-7=1414-7=7 So, 98 and 63 of greatest common divisor equals 7. Example 2, the greatest common divisor of 260 and 104 were obtained with the more subtractive loss technique. Solution: Since both 260 and 104 are even, first 2 reduction is used to get 130 and 52, then 2 and 65.  At this point 65 is odd and 26 is not odd, so the 65 and 26 are subtracted: 65-26=3939-26=1326-13=13 so, 260 and 104 of greatest common divisor equals 13 times the first step of about two 2, namely 13*2*2=52. Below is my own according to the algorithm to do an implementation, more cumbersome, but can realize the function, first put here, there is a better idea to update
 Packagecom.henu.test;ImportJava.util.HashMap;ImportJava.util.Map; Public classMathdemo { Public Static voidMain (string[] args) {//Enter the two number you requested heremap<string, object> map = Math (98,63);  for(Map.entry<string, object>Entry:map.entrySet ()) {System.out.println (Entry.getkey ()+"----"+Entry.getvalue ()); }            }    /*** More subtractive loss of operation *@paramA *@paramb *@return     */     Public StaticMap<string, Object> Math (intAintb) {Map<string, object> map =NewHashmap<>(); intCount = 0; inttemp = 0; intMax =0;  for(; a%2==0 && b%2==0;) {a=a/2; b=b/2; Count++; } Out: while(A-b>0 && A-!=b) | | (B>a && b-a! = a) | | A = = B | | B-a = = A | | A-B =b) {//While (a-b==0 | | b-a==0) {        if(A = =b) {temp=b;  BreakOut ; }Else if(B>a && b-a! =a) {temp= bA; b=temp; }Else if(A-b>0 && A-B! =b) {temp= A-C; A=temp; }Else if(B-a = =a) {temp=A;  BreakOut ; }Else if(A-B =b) {temp=b;  BreakOut ; }} Max= (int) Math.pow (2, Count) *temp; //calculate to the last value of aMap.put ("a", a); //calculate the value to the last BMap.put ("B", B); //indicates the number of times the joint is divisible by 2Map.put ("Count", Count); //the last greatest common divisor to be obtainedMap.put ("Max", Max); returnmap; }}

Original address: http://www.cnblogs.com/xiaoguaishou/p/7190489.html

Java implementation of the update impairment technique

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.