
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Euclidean Algorithm for calculating GCD in JavaScript
In mathematics, Euclid's algorithm, is a method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder.
The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number.
For example, 21 is the GCD of 252 and 105 (as 252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 252 − 105 = 147.
Since this replacement reduces the larger of the two numbers, repeating this process gives successively smaller pairs of numbers until the two numbers become equal. When that occurs, they are the GCD of the original two numbers.
By reversing the steps, the GCD can be expressed as a sum of the two original numbers each multiplied by a positive or negative integer, e.g., 21 = 5 × 105 + (−2) × 252.
We are required to write a JavaScript function that takes in two numbers and makes use of Euclid's algorithm to compute their GCD (greatest common divisor)
Example
Following is the code −
const num1 = 252; const num2 = 105; const findGCD = (num1, num2) => { let a = Math.abs(num1); let b = Math.abs(num2); while (a && b && a !== b) { if(a > b){ [a, b] = [a - b, b]; }else{ [a, b] = [a, b - a]; }; }; return a || b; }; console.log(findGCD(num1, num2));
Output
Following is the output on console −
21
- Related Articles
- Calculating Euclidean distance using SciPy
- Stein’s Algorithm for finding GCD in C++
- C++ Program to Implement Extended Euclidean Algorithm
- Calculating variance for an array of numbers in JavaScript
- Algorithm for matrix multiplication in JavaScript
- Find HCF of two numbers without using recursion or Euclidean algorithm in C++
- C Program for Basic Euclidean algorithms?
- C Program for Extended Euclidean algorithms?
- Python Program for Basic Euclidean algorithms
- Python Program for Extended Euclidean algorithms
- Calculating excluded average - JavaScript
- Solution for array reverse algorithm problem JavaScript
- Calculating factorial by recursion in JavaScript
- Calculating Josephus Permutations efficiently in JavaScript
- JavaScript algorithm for converting integers to roman numbers
