In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.