Get the App
SLTechnology News&Howtos  ›  Development  › 

How to find the greatest common divisor of two numbers by JavaScript

Shulou Source: shulou.com Published: 2022-06-01 06:09:35 10月02日 Update

Today, I would like to share with you the relevant knowledge about how to find the maximum common divisor of two numbers by JavaScript. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Idea: the greatest common factor, also known as the greatest common divisor, refers to the largest of the common divisors shared by two or more integers. First list all the factors of one of the numbers, and then start from the largest of these factors to determine whether to divide the other number. For example, 8 and 12, you can first list all the factors of 8, there are, 1, 2, 4, 8, and then, from big to small, divides 12 from 8, obviously, divides 12 by 4. Note that when you finish writing each function, you can check it.

Method 1: cyclic residual method:

/ / cyclic residual method

/ / to find the greatest common factor encapsulates a function, and the parameter aforme b is the number of two factors.

Function isyinzi (a, b) {

For (var I = a; I > = 1; iMel -) {

/ / find the factor of a number

If (a% I = = 0) {

/ / find the common factor in the factor of the first number, starting from large to small.

If (b% I = = 0) {

/ / returns the greatest common factor

Return i

}

}

}

}

Console.log (isyinzi (3,12))

Method 2: toss and turn (Euclidean algorithm), write recursively (call yourself)

/ / the maximum common divisor of two numbers is given to encapsulate a function, and the parameter aforme b is two numbers.

Function isyinzi (a, b) {

/ / define an intermediate variable in exchange for a _ a must be the number with the largest number of two.

Var z = 0

If (a < b) {

Z = a

A = b

B = z

}

/ / the Euclidean algorithm is expressed by recursion, and the idea is to take the inexhaustible.

/ / a/b=c for the first time The a value of the second time d is the value of the first time b, and the value of b

/ / is the value of the first d, and so on until the value of d is 0

Function isdigui () {

If (a% b! = 0) {

Return a% b

} else return b

A = b

Return a% isdigui () / / Recursive, call yourself until the condition of judgment is reached

}

Return isdigui ()

}

Console.log (isyinzi (12,8)) / / call function

The ultimate: essentially the same: using the ternary operator

Function isgy (a, b) {

Return a% b = 0? B: isgy (b, a% b)

}

Console.log (isgy (4,2))

The results are as follows: the maximum common divisor of 122.8 is 4.

These are all the contents of the article "how to find the greatest common divisor of two numbers by JavaScript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Maximum factor two common divisor maximum common divisor number function knowledge article that is for the first time recursion content parameters methods algorithms Euclid Euclid Reed encapsulation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Apple OPPO Reno Shulou Technology