This section corresponds to the video

The original:

There are twelve eighths today. How much does the covenant get?

Answer: two-thirds.

And forty-nine out of ninety-one. How much does the covenant get?

Seven out of thirteen.

Reduction of a fraction

If there are two parts, it can also be four eighths; if there are two parts, it can also be one half. Though the terms are different, as for the number, it is the same thing. The dharma reality moves in different ways, so it is for the practitioner to treat the parts first.

The art said: can be half and half. Not half, sub denominator, the number of children, with less reduction, more phase reduction, and so on. It is approximated by equal numbers.

Equal numbers are equal, that is, divide and also. All the subtractions are equal numbers of overlap, so equal numbers are equal.

The simple definition of “divisor” is to make the numerator and denominator of a fraction have no common divisor other than 1 by calculation.

The method mentioned in the division is sorted out step by step, that is:

If the numerator and denominator are both even, divide by 2 and repeat the process until the numerator and denominator are odd.

2. If there is an odd number in the numerator and denominator, subtract the smaller number from the larger number;

3. Compare the difference obtained in the previous step with the subtraction of the previous step, and subtract the smaller number from the larger number;

4. Repeat step 3 until the subtraction equals the difference.

5. Divide the numerator and denominator in Step 2 by the difference obtained in Step 4.

This is the first time that we have encountered a problem with complicated logic in the calculation process. It is not appropriate to describe this process with expressions, but with pseudocode.

Pseudo code:

Divide (numerator and denominator) {while (numerator and denominator are both even) {numerator/denominator /2} if (numerator and denominator include odd) {small value = small difference in numerator and denominator = (denominator - numerator) absolute value while (small value! = difference) {new minor value = smaller value, smaller number of difference new difference = (smaller value - difference) absolute value if (new minor value = new difference) {common divisor = new minor value out of the loop} small value = new minor value difference = new difference} numerator = numerator/common divisor denominator = Common divisor} return numerator/denominator}Copy the code

The next step is to implement this functionality with actual code.

In programming languages, there is no primitive data type for fractions. We can create a class or structure that represents fractions and use this fraction class as the input and output type. The simpler and more crude way is to take the numerator and denominator directly as two integer data as the input parameter and return value.

Here I take JSON with the numerator and denominator fields, which is essentially the second way.

Incorrect input and special input need to be handled.

The input contains numerator and denominator values, so you need to determine that the denominator cannot be 0. (Strongly typed languages can filter incoming parameter types without requiring a separate integer, so they don’t filter input for integers at this point.) When the numerator is 0, it’s also 0; When the numerator is equal to the denominator, you divide it by 1.

In addition, the original calculation method does not take into account negative numbers, so we need to consider processing the input negative fractions. In order to deal with the negative fraction as a parameter, we can calculate the greatest common divisor when the numerator and denominator have odd numbers by taking the absolute value, so as not to be affected by positive and negative numbers. (Both positive and negative numbers don’t affect dividing by 2, so if the numerator and denominator are even, you don’t have to worry about it.) The previous special input judgment that “the numerator is equal to the denominator” needs to be changed to “the absolute value of the numerator and the denominator are equal”, and the corresponding processing should be changed to return “the numerator/denominator”, which is 1 including the sign of plus and minus.

Those who need code source files can follow my wechat public account jianming-95

Input: {numerator, denominator}

Output: {divisor numerator, divisor denominator}

Basic version code:

Function chOne02(input){// Input in JSON format: den let num = input. let den = input.den; If (den === 0){return {"code":"001"," MSG ":"0" }; } the if (num = = = 0) {/ / molecules to 0, 0 returned directly results return {" res ": 0}; } if(math.abs (num) === math.abs (den)){return{"res":num/den}; if(math.abs (num) === math.abs (den)); } while(num % 2 === 0 && den % 2 === 0){if (num % 2 == 0 && den % 2 === 0); den = den / 2; } if(num % 2 ! =0 || den % 2 ! Math.abs(num) < math.abs (den)? Math.abs(num) : Math.abs(den); Math.abs(num) - math.abs (den)); // Let dif = math.abs (num) - math.abs (den); // Calculate the absolute value of the difference between the numerator and denominator. // Declare a maximum common visor variable divisor while(min! // newMin = min < dif? // newMin = min < dif? min : dif; Let newDif = math.abs (min-dif); let newDif = math.abs (min-dif); If (newMin === newDif){if(newMin === newDif){divisor = newDif; // The end of the loop is subtraction = difference, is the greatest common divisor break; } min = newMin; dif = newDif; } num = num / divisor; Den = den/divisor; den = den/divisor; } return {"num":num,"den":den}; // Return result in JSON format}Copy the code

Calling code and running results:

Let input = {"num":49,"den":-91}; // Create the input variable console.log(chOne02(input)); [Running] node "d:\WorkSpace\jsWork\NCMA\chOne\ chone02.js "{num: 7, den: -13} [Done] exited with code=0 in 0.14 secondsCopy the code

The above divisor process can be summarized as two operations: find the greatest common divisor of the numerator and denominator, and divide the numerator and denominator by the greatest common divisor.

The greatest common divisor is something that we might use in other places, so we can extract it and encapsulate it into a method that we can reuse.

Obviously, the method of finding the greatest common divisor, the input is two integers that require the common divisor, and the output is the greatest common divisor.

The input and output of the partition method remain unchanged.

Dividing method:

Input: {numerator, denominator}

Output: {divisor numerator, divisor denominator}

Method for finding the greatest common divisor:

Input: {integer 1, integer 2}

Output: {maximum common divisor}

Upgrade code:

Dividing method:

Function chOne03(input){// Input in JSON format: den let num = input. let den = input.den; If (den === 0){return {"code":"001"," MSG ":"0" }; } the if (num = = = 0) {/ / molecules to 0, 0 returned directly results return {" res ": 0}; } if(num === den){return{"res":1}; } let divisor = getDivisor({"num1":num,"num2":den}).divisor; Num = num/divisor; Den = den/divisor; den = den/divisor; return {"num":num,"den":den}; // Return result in JSON format}Copy the code

Find the greatest common divisor:

Function getDivisor(input){getDivisor(input){getDivisor(input){getDivisor(input){getDivisor(input){getDivisor(input){getDivisor(input); let num2 = input.num2; If (num1 = = = 0 | | num2 = = = 0) {/ / two Numbers contain 0, the greatest common divisor is 0 return {" divisor ": 0}} the if (Math. Abs (num1) = = = Math. Abs (num2)) {/ / two equals the number of absolute value, Return {"divisor": math.abs (num1)}; } let divisor = 1; While (num1%2 === 0 && num2%2 === = 0){// If both numbers are even, divide by 2 num1 = num1/2; num2 = num2 / 2; divisor = divisor * 2; } if(num1%2! = 0 || num2 % 2 ! Math.abs(num1) < math.abs (num2)? Math.abs(num1) : Math.abs(num2); // let dif = math.abs (math.abs (num1) - math.abs (num2)); // Calculate the absolute value of the difference while(min! // newMin = min < dif? // newMin = min < dif? min : dif; Let newDif = math.abs (min-dif); let newDif = math.abs (min-dif); If (newMin === newDif){if(newMin === newDif){divisor = divisor * newDif; // The loop ends with the subtraction = difference, which is the largest common divisor break; } min = newMin; dif = newDif; } } return{"divisor":divisor}; }Copy the code

Calling code and running results:

Let input = {"num":49,"den":91}; // Create the input variable console.log(chOne03(input)); [Running] node "d:\WorkSpace\jsWork\NCMA\chOne\ chone03.js "{num: 7, den: 13} [Done] exited with code=0 in 0.115 secondsCopy the code