如何解决 JavaScript 中潜在的无限 while 循环。在某种情况下,while 循环适用于较小的数字但无法处理较大的数字? [重复]

问题描述 投票:0回答:1

我正在写一个函数,它可以找到给定数字范围之间的最小公倍数。也就是说,一个函数返回一个数字,给定数字范围内的所有数字都可以整除而没有余数。

我已经能够想出一个有效但包含 while 循环的代码,但是,当我增加传递的数字范围时,我的 while 循环中断并返回“潜在的无限循环”

问题 我想写一个函数,找到一个数字范围的最小公倍数,给定两个参数,较小的数字是较低的范围,较高的数字是数字的较高范围。

例如。当给出 ([1,3]) 时,答案应该是 6 原因: ([1,3]) augments传入函数,简单给出[1,2,3]的范围,1、2、3的最小公倍数是6,也就是说6是最小的1,2 和 3 都将被除且没有余数的数。

下面是我用 JavaScript 编写的代码

const sum = (x,y) => x+y //
const myRangeArr = [ ]


function smallestCommonMultiple(arr) {
    
    let sortedArr = arr.concat([]).sort((a,b)=> a-b) //should incase the numbers do not  follow numeric asending order
  
for (let i = sortedArr[0] ; i<= sortedArr[1]; i++){
  myRangeArr.push(i)
} // this generates the full range of numbers

let smallestMultiple = myRangeArr[myRangeArr.length -1]


const result = () =>  myRangeArr.map( eachNum => smallestMultiple % eachNum).reduce(sum,0)
// when each number in myRangeArr is mapped and it returns their modulus, then I call the reduce function on it to sum all the modulus, if that number is a multiple of all the range of numbers, that means my answer will be zero (0)


while ( result() !== 0){
    smallestMultiple+= sortedArr[1] //since sortedArr[1] is the highest number in myRangeArr. Therefore let smllestMultiple keep increasing by the higest range number till the result() eventuall becomes equals to zero (0)
}

  return smallestMultiple;
}

console.log(smallestCommonMultiple([1,3])); // this works and outputs 6
console.log(smallestCommonMultiple([5,1])); // this works and outputs 60
console.log(smallestCommonMultiple([2,1])); // this fails and outputs 60...Answer should be 2
console.log(smallestCommonMultiple([23,18])); // this fails and outputs "Potential infinite loop detected on line 21"... answer should be 6056820

type here

没有注释的代码...


const sum = (x,y) => x+y
const myRangeArr = [ ]

function smallestCommonMultiple(arr) {
    let sortedArr = arr.concat([]).sort((a,b)=> a-b) 
  
    for (let i = sortedArr[0] ; i<= sortedArr[1]; i++){ myRangeArr.push(i) } 

    let smallestMultiple = myRangeArr[myRangeArr.length -1]

const result = () =>  myRangeArr.map( eachNum => smallestMultiple % eachNum).reduce(sum,0)

       while ( result() !== 0){ smallestMultiple += sortedArr[1]}

      return smallestMultiple;
}


console.log(smallestCommonMultiple([1,3])); // pass
console.log(smallestCommonMultiple([5,1])); // pass
console.log(smallestCommonMultiple([2,1])); // fail
console.log(smallestCommonMultiple([23,18])); // fail

拜托,有没有其他更好的方法可以解决这个问题,或者有什么我没有考虑到的东西(当然我知道一定有)所以我可以避免这些失败的情况?

谢谢!

javascript algorithm function loops while-loop
1个回答
0
投票

两个数的 LCM 是它们的乘积除以它们的最大公约数 (GCD)。可以通过归约运算计算多个数字的 LCM,得到数组中前一个 LCM 和下一个数字的 LCM。

可以使用欧几里德算法有效地计算 GCD。

const gcd = (a, b) => b ? gcd(b, a % b) : a;
function lcm(a) {
  const l = Math.min(a[0], a[1]), r = Math.max(a[0], a[1]);
  return [...Array(r-l+1)].map((_,i)=>i+l).reduce((a, b) => a / gcd(a, b) * b, 1);
}
console.log(lcm([1,3]));
console.log(lcm([23,18]));

© www.soinside.com 2019 - 2024. All rights reserved.