Smallest Common multiple

Source: Internet
Author: User
Tags greatest common divisor

Topic

Find the least common multiple that can be divisible by two consecutive numbers of given parameters and 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.

smallestCommons([1, 5])A number should be returned. smallestCommons([1, 5])should return 60. smallestCommons([5, 1])should return 60. smallestCommons([1, 13])should return 360360.

Ideas

Problem Analysis: If the interval is [1,5], the value is 60, that is, the value to be calculated is 1,2,3,4,5 common multiple (3x4x5)

The two numbers are not necessarily sorted numerically, as the topic shows. So let's do a little bit of sort work so that 69 is always 69 and not boring 96

  Arr=arr.sort (function (A, b) {return-a    ;  });

Two digital least common multiple: a*b/(AB Two number of greatest common divisor)

Find the least common multiple of two numbers and the third number and the least common multiple of the first two numbers for least common multiple. I guess you're dizzy now, and if you're not dizzy, I'm dizzy. Just look at the example below.

6,9,15 a value of three least common multiple. First the 6,9 least common multiple through the above formula to find out is 36, and then 36 and 15 of the least common multiple to find out is 180 (presumably)

This 180 is the least common multiple of the 6,9,15 three numbers, if there is a fourth fifth one ... Still follow the above to continue to beg.

As for greatest common divisor, do you think I will tell you? No, but the great ancestors will. Data: Euclidean algorithm

Using the above theory, the numerical method of arr[0] to arr[1] can be obtained least common multiple

1   varNum=arr[0];2    for(varI= arr[0]+1;i<=arr[1];i++){3NUM*=I/GCD (Num,i);4   }5 //Euclidean algorithm seeking greatest common divisor6 functiongcd (m,n) {7   if(m%n===0)returnN;8   returnGCD (n,m%n);9}

Code:

1 functionsmallestcommons (arr) {2Arr=arr.sort (function(A, b) {3     returnA-b;4   });5   varNum=arr[0];6    for(varI= arr[0]+1;i<=arr[1];i++){7NUM*=I/GCD (Num,i);8   }9   returnnum;Ten } One functiongcd (m,n) { A   if(m%n===0)returnN; -   returnGCD (n,m%n); -}

Smallest Common multiple

Related Article

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.