Concept: The greatest common factor: also known as the greatest common divisor, the greatest common factor, refers to two or more integers common divisor of the largest one. Divisor: Also called factor. If the quotient of an integer a divided by an integer b(b≠0) is exactly an integer with no remainder, we say a is divisible by B, or b is divisible by A. A is called a multiple of b and b is called a divisor of A.
Logic steps: the first algorithm logic: 1, the greatest common factor, is the largest divisor of two numbers can be exactly divisible. 2. Divisible means the remainder is 0. We can use the remainder symbol % in JS operation. When the remainder of both integers is 0, the divisor is stored in a variable. At the end of the loop, the divisor is the largest common divisor.
function maxcommonDivisor(n1, n2) {
n1 = n1 && parseInt(n1);
n2 = n2 && parseInt(n2);
if(n1 - n2 > 0) {
// Compare the sizes of n1 and n2, using deconstruction assignment to swap n1 and n2
[n1, n2] = [n2, n1];
}
let sum;
for(let i = 0; i <= n1; i++) {
if(n1 % i == 0 && n2 % i == 0) { sum = i; }}return sum;
}
Copy the code
The second algorithm logic :(toss and turn division) 1, toss and turn division is also called Euclid algorithm. Divide with the divisor and remainder repeatedly, when the remainder is 0, take the divisor of the current formula as the greatest common divisor.
function maxcommonDivisor(n1, n2) {
n1 = n1 && parseInt(n1);
n2 = n2 && parseInt(n2);
if(n1 < n2) {
[n1, n2] = [n2, n1];
}
/* If n1 and n2 are 0, then n2 is the largest common divisor of the two integers. If it is not 0, the function itself is called so that the divisor and remainder continue to perform the remainder operation */
if(n1 % n2 == 0) {
return n2;
} else {
return arguments.callee(n2, n1 % n2); }}Copy the code
The third algorithm logic :(more loss phase subtraction) 1, any given two positive integers; See if they’re all even. If so, use 2 to reduce; If no, go to Step 2. 2. Subtract a smaller number from a larger number, then compare the difference with the smaller number and reduce the number by the larger number. Continue until the resulting subtraction and difference are equal. 3. The product of the number of 2’s cancelled in step 2 and the intermediate number in step 2 is the greatest common divisor.
function maxcommonDivisor(n1, n2) {
n1 = n1 && parseInt(n1);
n2 = n2 && parseInt(n2);
// Declare the variable sum to record the number of times that it is divided exactly by 2
let sum = 1;
// Use the while loop to mod two integers by 2
while(n1 % 2= =0 && n2 % 2= =0) {
n1 = n1 / 2;
n2 = n2 / 2;
sum *= 2;
}
// Compare the size of n1 and n2, using deconstruction assignment, so that the larger number is equal to n1
if(n1 < n2) {
[n1, n2] = [n2, n1];
}
// Subtract a smaller number from a larger number
let poor = n1 - n2;
// Use the while loop to determine if the difference is equal to n2. If not, continue to subtract the difference from the smaller number
while(poor ! = n2) {if(poor < n2) {
[n1, n2] = [n2, poor];
} else {
[n1, n2] = [poor, n2];
}
poor = n1 - n2;
}
// The difference multiplied by a number of 2's is the greatest common divisor
return poor * sum;
}
Copy the code