Smallest Common Multiple

题目

找出能被两个给定参数和它们之间的连续数字整除的最小公倍数。

范围是两个数字构成的数组,两个数字不一定按数字顺序排序。

例如对 1 和 3 —— 找出能被 1 和 3 和它们之间所有数字整除的最小公倍数。

smallestCommons([1, 5]) 应该返回一个数字。

smallestCommons([1, 5]) 应该返回 60。

smallestCommons([5, 1]) 应该返回 60。

smallestCommons([1, 13]) 应该返回 360360。



思路

题目解析:假如区间为[1,5],所求的数值是60,也就是说需要求出来的值是的1,2,3,4,5的公倍数(3x4x5)

由题目可见,两个数字不一定按数字顺序排序。所以我们先做一点微小的排序工作,让69始终是69而变不成无趣的96

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

两个数字最小公倍数的求法:A*B/(AB两数的最大公约数)

求出两个数的最小公倍数再套第三个数与前两个数的最小公倍数求最小公倍数。我猜你现在已经看晕了,如果你没晕我晕了。看下面的实例就好了。

6,9,15三个数值的最小公倍数。先把6,9的最小公倍数通过上面的公式求出来就是36,然后再将36和15的最小公倍数求出来是180(大概是)

这个180就是6,9,15这三个数的最小公倍数,如果有第四个第五个...依旧按照上面的写法继续求。

至于最大公约数怎么求,你以为我会告诉你吗?不会,但伟大的先人会。资料:欧几里得算法

运用上面的理论,将arr[0]到arr[1]的数值代入算法中即可得出最小公倍数

1   var num=arr[0];
2   for(var i= arr[0]+1;i<=arr[1];i++){
3     num*=i/gcd(num,i);
4   }
5 //欧几里得算法 求最大公约数
6 function gcd(m,n){
7   if(m%n===0)return n;
8   return gcd(n,m%n);
9 }

代码:

 1 function smallestCommons(arr) {
 2   arr=arr.sort(function(a,b){
 3     return a-b;
 4   });
 5   var num=arr[0];
 6   for(var i= arr[0]+1;i<=arr[1];i++){
 7     num*=i/gcd(num,i);
 8   }
 9   return num;
10 }
11 function gcd(m,n){
12   if(m%n===0)return n;
13   return gcd(n,m%n);
14 }
时间: 2024-10-14 05:36:14

Smallest Common Multiple的相关文章

Smallest Common Multiple FreeCodeCamp

题目:找出能被两个给定参数和它们之间的连续数字整除的最小公倍数.  范围是两个数字构成的数组,两个数字不一定按数字顺序排序. 分析:首先题目的意思求一个连续数列的所有数字的最小公倍数,这连续的数字序列可能递增,也可能递减,有两种情况,为了使代码简洁, 我们将其变为一种情况,也就是递增数列,这样避免重复写递减数列的情况.再一看,这里说了参数是数组,那么可以使用sort()方法排序.    这道题其实只要懂得怎么求两个数之间的最小公倍数,就可以求这个序列的最小公倍数了.因为,最小公倍数也是一个数字,

hdu 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54584    Accepted Submission(s): 20824 Problem Description The least common multiple (LCM) of a set of positive integers is

HDUJ 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29480    Accepted Submission(s): 11136 Problem Description The least common multiple (LCM) of a set of positive integers is

HDU 1019 Least Common Multiple (最小公倍数)

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30285    Accepted Submission(s): 11455 Problem Description The least common multiple (LCM) of a set of positive integers is

1019 Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 51959    Accepted Submission(s): 19706   Problem Description The least common multiple (LCM) of a set of positive integers is the smallest positiv

HDU 1019 Least Common Multiple 数学题

Problem Description The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input will consist of multiple prob

HDU1019 Least Common Multiple(多个数的最小公倍数)

The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. InputInput will consist of multiple problem instances. The fi

Least Common Multiple HDU1019

The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. InputInput will consist of multiple problem instances. The fi

K - Least Common Multiple

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. Fo