Least Common Multiple of an array values using Euclidean Algorithm


I want to calculate the least common multiple of an array of values, using Euclideans algorithm

I am using this pseudocode implementation: found on <a href="https://en.wikipedia.org/wiki/Euclidean_algorithm" rel="nofollow">wikipedia</a>

function gcd(a, b) while b ≠ 0 t := b; b := a mod b; a := t; return a;

My javascript implementation is such

function smallestCommons(arr) { var gcm = arr.reduce(function(a,b){ let minNum = Math.min(a,b); let maxNum = Math.max(a,b); var placeHolder = 0; while(minNum!==0){ placeHolder = maxNum; maxNum = minNum; minNum = placeHolder%minNum; } return (a*b)/(minNum); },1); return gcm; } smallestCommons([1,2,3,4,5]);

I get error, on my whileloop


Infinite loop


<strong>EDIT</strong> Some corrections were made, at the end of gcm function, I used 0 as the initial start value, it should be 1, since you can't have a gcm from 0.

<strong>EDIT2</strong> The expected output should be 60, since thats the least common multiple of 1,2,3,4,5


Did you intentionally tangle all variables and operator sequence? ;-)

while(minNum!==0){ placeHolder = minNum; minNum = maxNum % minNum; maxNum = placeHolder; } //here maxNum = GCD(a,b) return (a*b) / (maxNum); //LCM


<h3>With ES6</h3> let gcd = (a, b) => a ? gcd(b % a, a) : b; let lcm = (a, b) => a * b / gcd(a, b);

Then use reduce on given array of integers:

[1, 2, 3, 4, 5].reduce(lcm); // Returns 60 <hr /><h3>With ES5</h3> var gcd = function (a, b) { return a ? gcd(b % a, a) : b; } var lcm = function (a, b) { return a * b / gcd(a, b); }

Then use reduce on given array of integers:

[1, 2, 3, 4, 5].reduce(lcm); // Returns 60


  • Array that takes user input to fill but does not allow duplicate elements
  • C# Entity Framework select max after where filter of not nullable field
  • Input data type check in loop not working as intended (C++)
  • Could not find the gcm.jar error when creating App Engine backend => suggests installing deprecat
  • strategy for syncing Android app database with a remote API
  • Cloudflare and Firebase
  • How do you disable Android Library Update?
  • getting java.lang.ClassCastException: class java.lang.String in running a simple MapReduce Program
  • static library has big size
  • Failed to resolve: support-core-ui
  • float vs double comparison [duplicate]
  • lodash _.get function in typescript
  • Better algorithm for if condition
  • Python regex catastrophic backtracking
  • How to reduce the time delay to reach run method of Runnable class using ExecutorService Java
  • Performance of System.IO.ReadAllxxx / WriteAllxxx methods
  • Plotly and ggplot with facet_grid in R: How to to get yaxis labels to use ticktext value instead of
  • Android GCM push notification without server OR GCM push notification using Microsoft SQL(Not Mysql)
  • How to insert an Image in WORD after a bookmark using OpenXML
  • Hadoop shuffle uses which protocol?
  • finding greatest prime factor using recursion in c
  • Runtime error in UVA Online Judge [closed]
  • Imageloader not loading image on real device
  • JSON - slashes not escaping
  • Inversing an interpolation of rotation
  • C# fibonacci function returning errors
  • How does this usort cmp function actually work?
  • D3 get axis values on zoom event
  • Custom validator control occupying space even though display set to dynamic
  • JSON response opens as a file, but I can't access it with JavaScript
  • HTML download movie download link
  • Updating server-side rendering client-side
  • How to delete a row from a dynamic generate table using jquery?
  • Buffer size for converting unsigned long to string
  • using HTMLImports.whenReady not working in chrome
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Authorize attributes not working in MVC 4
  • apache spark aggregate function using min value
  • EntityFramework adding new object to nested object collection