Smallest Common multiple
Find the least common multiple that can be divisible by two of the given parameters and the successive numbers between them.
The range is an array of two numbers, and two numbers are not necessarily sorted in numerical order.
For example, 1 and 3--find least common multiple that can be divisible by 1 and 3 and all the numbers between them.
Smallest Common multiple
Ideas:
(1) Find out the maximum value and the minimum value of the given value;
(2) The maximum value is multiplied by 1 to 100000 to load the array Maxarr;
(3) The given two values and their median number are sequentially loaded into the array newarr;
(4) Divide the maximum number of multiples of each and newarr, and then compare;
(5) Returns the first minimum value that divides all the elements of the newarr into one;
Knowledge Points:
(1) The Math.max.apply (null, Array) function can find the maximum value of a set of numbers;
(2) This value can be set freely, originally want to let him for newarr each element multiplication of numeric value, but sometimes the value is too large, easy to cause collapse;
1 for (var k = 0; k < 100000; k++) {2 // multiply the maximum value by 1 to 100000 to load the array into the Maxarr 3 Maxarr.push (Max * (k + 1)); 4 }
Code:
1 functionsmallestcommons (arr) {2 varNEWARR = [];3 varMaxarr = [];4 varnum = 1;5 varmax = Math.max.apply (NULL, arr);6 varmin = Math.min.apply (NULL, arr);7 8 9 for(vark = 0; K < 100000; k++) {Ten //multiply the maximum value by 1 to 100000 to load the array into the Maxarr OneMaxarr.push (Max * (k + 1)); A } - for(vari = min; I < Max + 1; i++) { - //loads the given two values and their intermediate numbers sequentially into the array newarr the Newarr.push (i); - } - - for(varj = 0; J < Maxarr.length; J + +) { + varb = 1; - for(varm = 0; M < newarr.length; m++) { + //Divide the multiples of the maximum value from the values of the newarr, and then compare A if(Maxarr[j]% newarr[m]!== 0) { atb = 0; - } - } - if(b = = 1) { - //returns the first minimum value that can divide all newarr elements - returnMaxarr[j]; in } - } to } +Smallestcommons ([1, 13]);
FCC Intermediate algorithm problem least common multiple