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 13:10:35 09月24日 Update

In this article, the editor introduces in detail "JavaScript how to find the maximum common divisor of two numbers". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "JavaScript how to find the maximum common divisor of two numbers" can help you solve your doubts.

In JavaScript, you can use the function statement and if statement to work with the "%" and "= =" operators to find the maximum common divisor of two numbers. The syntax is "function gcd (xmemy) {if (x%y===0) {return y;} return gcd (ymagin x% y)}".

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

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

In JavaScript, find the maximum common divisor of two numbers

Examples are as follows:

/ / function: find the maximum common divisor / / Parameter: X, y number / / return value: number function gcd (x, y) {if (x% y = 0) {return y;} return gcd (y, x% y) / / ternary operator: / / return x% y = 0? Y: gcd (y, x% y);} var res = gcd (5,20); console.log (res); / / 5

Other methods:

1. Maximum common divisor-cyclic remainder

/ / maximum common divisor-Recursive remainder / / function: maximum common divisor of two numbers / / Parameter: xPowery number / / return value: maximum common divisor number function gcd (xPowery) {/ / compare the size of two numbers, take the smaller number var min = x

< y ? x : y ; //从大到小循环找第一个公约数 for(var i = min ; i >

= 1; var res -) {/ / determine whether it is a common divisor if (x% I = = 0 & & y% I = = 0) {return I;} var res = gcd (5,20); [xss_clean] (res); / / 5

two。 Maximum common divisor-Euclid algorithm-recursive implementation

F method: the large number makes the remainder of the decimal, and repeat the process until the remainder is 0.

/ / maximum common divisor-Euclid algorithm-Recursive implementation / / Recursion: the remainder of large numbers to decimals Repeat this process until the remainder is 0 / / function: find the greatest common divisor / / Parameter: mMagazine n number / / return value: number function gcd (mMagna n) {/ / can large numbers be divided by decimals var max = m > n? M: n; var min = m < n? M: n; if (max% min = 0) {/ / if divisible, the decimal is the greatest common divisor return min;} else {return f (min, max% min);}} var res = gcd (5,20); [xss_clean] (res) / / 5 after reading here, the article "how to find the greatest common divisor of two numbers by JavaScript" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Common divisor maximum maximum common divisor two remainder decimal function parameter large number article recursion content method algorithm statement procedure operator Euclid Euclid Reid Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info NVidia vpn Apple