In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
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.