Javascript - Tìm ước chung lớn nhất của hai số dương

Tìm ước chung lớn nhất của hai số dương

Viết chương trình JavaScript để tìm ước chung lớn nhất  của hai số dương.

Ví dụ:

JavaScript: Find the greatest common divisor of two positive numbers

Mã nguồn:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Recursive function to find the GCD of two numbers</title>
 <script>
var gcd = function(a, b) {
    if ( ! b) {
        return a;
    }

    return gcd(b, a % b);
};
document.write(gcd(2154, 458));
  </script>
</head>
<body>

</body>
</html>

Xem ví dụ

Lưu đồ thuật toán:

Flowchart: Flowchart: JavaScript recursion function- Find the greatest common divisor(gcd) of two positive numbers


Tư vấn lộ trình CNTT 🤖