我如何创建一个for循环,该循环递增一个数字,直到找到余数为0的那个?

时间:2018-10-04 11:13:28

标签: javascript algorithm for-loop

我有一个函数,该函数应该找到所提供参数的最小公倍数,该参数可以被两个参数以及它们之间的参数均分。

数组始终由两个数字组成,不能按数字顺序排列。

例如。值[1, 3]将返回6,或者[1, 5]将是60。

  1. 我了解,首先我需要找到数组中的最小数字和最大数字。
  2. 然后我必须将所有数字(包括它们在内的最小和最大数字)放入一个数组中。
  3. 然后我需要以某种方式创建一个for循环,该循环会递增一个数字,直到找到除以数组中每个数字的余数为0为止。

我理解了第三部分,但是不知道如何实现。任何帮助和提示将不胜感激。

到目前为止,这是我的代码:

function smallestCommons(arr) {
  let minNum = Math.min(...arr);
  let maxNum = Math.max(...arr);
  let mySeq = [];
  for(let i = minNum; i <= maxNum; i++){
    mySeq.push(i);
  }
  // stuck here
}

谢谢。

5 个答案:

答案 0 :(得分:2)

一种实用的方法。

items

答案 1 :(得分:0)

根据您的想法,我可以创建一个像这样的函数

function smallestCommons(arr) {
  let minNum = Math.min(...arr);
  let maxNum = Math.max(...arr);
  let mySeq = [];
  for(let i = minNum; i <= maxNum; i++){
    mySeq.push(i);
  }
  // stuck here
  let smallestCommon = maxNum;
  let found = false;
  while(!found){
    var j;
    for(j = 0; j< mySeq.length; j++){
      if(smallestCommon % mySeq[j] != 0) break;
    }
    if(j == mySeq.length){
      found = true;
    }else{
      smallestCommon ++;
    }
  }

  return smallestCommon;
}

smallestCommons([1, 3]) //return 6
smallestCommons([1, 5]) //return 60

但是,我认为对于这个问题,还有更好的方法。

答案 2 :(得分:0)

由于我不是JS人,所以我可以给您伪代码。

lcm = 1
list =[1,2,3]

for i in len(list)-1:
   lcm = lcm*list[i+1]/gcd(lcm,list[i+1])

print(lcm)

假设我们需要找到1到n之间的1厘米数字

否则,我们需要使用列表的前两个数字来初始化lcm。

答案 3 :(得分:0)

嘿,这适用于您的方案的代码。

        let array = [1,5];

        let returnValFunction = (arr) => {
            try {
                let minNum = Math.min(...arr);
                let maxNum = Math.max(...arr);
                let returnVal = false;
                let i = 1;
                while (!returnVal) {
                    let divingNum = maxNum * i;
                    let checkVal = true;
                    for (let a = maxNum; a >= minNum; a--) {
                        console.log('a', a);
                        console.log(Number.isInteger(divingNum / a));
                        !Number.isInteger(divingNum / a) ? checkVal = false : '';
                    }
                    if (checkVal) returnVal = divingNum;            
                    i++;
                };
                return returnVal;
            } catch(err) {
                console.log(err);
            }
        }

        let consoleValue = returnValFunction(array);
        console.log('consoleValue', consoleValue);

答案 4 :(得分:0)

这是一个需要解决的难题。我以这种方式解决了这个问题,但是我敢肯定还有更好的方法。

我首先计算了两个最大数字之间的lmc。

然后,我比较此数字乘以1,然后乘以2,然后乘以3,依此类推,对下一个最低数字(最大数字-2)取模,直到得出0。我一直在计算然后取模(最大数-3),直到(最大数-n)= arr [0];

function lcm(nb1, nb2)
{
    let i = 1;
    let j = 1;
		
    while (true)
    {
        for (j = 1; j <= i; ++j)
        {
            if ((nb1 * i) == (nb2 * j))
                return (nb1 * i);
        }
        ++i;
    }
}
	
function smallestCommons(arr)
{
    if (arr.length == 2)
    {
        biggestNumber = arr[1];
        if (biggestNumber > 2)
        {
            let biggestCommon = lcm(arr[1] - 1, arr[1]);
            let mul = 1;
            for (let num = biggestNumber - 2; num > arr[0]; --num)
                while (((biggestCommon * mul) % num) != 0)
                    ++mul;
            return (biggestCommon * mul);
        }
        else
            return (biggestNumber);
    }
}
	
console.log(smallestCommons([1, 5]));
console.log(smallestCommons([1, 3]));
console.log(smallestCommons([1, 42]));
console.log(smallestCommons([7, 10]));
console.log(smallestCommons([4, 5]));