FCC Intermediate algorithm problem least common multiple

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.