Home > Web Front-end > JS Tutorial > body text

How to calculate GCD of two or more numbers/arrays in JavaScript?

WBOY
Release: 2023-09-10 23:13:11
forward
958 people have browsed it

如何在 JavaScript 中计算两个或多个数字/数组的 GCD?

The greatest common divisor (GCD) of two or more numbers, also called the greatest common factor (GCF) or highest common factor (HCF), is The largest positive integer that divides a given value without a remainder. In other words, GCD is the largest divisor of two numbers.

For example, the GCD of 24 and 36 is 12.

How to calculate two numbers?

There are several different ways to calculate the GCD of two numbers, but the most common method is the Euclidean algorithm.

Euclidean algorithm is an iterative method that starts with two numbers a and b and finds the GCD of a and b. The basic idea of ​​Euclidean's algorithm is to continuously subtract a smaller number from a larger number until the two numbers are equal.

  • For example, let us find GCD for 24 and 36 using Euclidean algorithm.

  • Starting with 24 and 36, we subtract the smaller number (24) from the larger number (36) to get 12.

  • Then, we subtract the smaller number (12) from the larger number (24) to get 12.

  • Since these two numbers are now equal, we have found GCD! The GCD in this example is 12.

How to calculate GCD for more than two numbers?

You can also use the Euclidean algorithm to calculate the GCD of more than two numbers. The basic idea is the same as before, but instead of subtracting the smaller number from the larger number, you subtract the GCD of two numbers from the larger number.

  • For example, we find the GCD of 24, 36, and 48.
  • First, we use Euclidean algorithm to find the GCD of 24 and 36, which is 12.

  • Then, we use Euclidean algorithm again The algorithm finds the GCD of 36 and 48, which is 12.

  • Finally, we use the Euclidean algorithm for the last time to find the GCD of 48 and 12, which is 12.

  • Since the GCD of 24, 36 and 48 is 12, we can stop here.

Example

This is a complete working code example of how to calculate the GCD of two or more numbers in JavaScript.




   Examples

Calculating GCD (Greatest Common Divisor)

Copy after login

Conclusion

In this article, we learned how to calculate the Greatest Common Divisor (GCD) of two or more numbers using Euclidean Algorithm.

The above is the detailed content of How to calculate GCD of two or more numbers/arrays in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!