在 JavaScript 中计算两个或三个数字的 LCM
Calculate the LCM of two or three numbers in JavaScript
我使用以下代码来确定两个或三个数字的最大公分母(GCD):
Math.GCD = function(numbers) {
for (var i = 1 ; i < numbers.length ; i++){
if (numbers[i] || numbers[i] === 0)
numbers[0] = twogcd(numbers[0], numbers[i]);
}
return numbers[0];
function twogcd(first, second) {
if (first < 0) first = -first;
if (second < 0) second = -second;
if (second > first) {var temp = first; first = second; second = temp;}
while (true) {
first %= second;
if (first == 0) return second;
second %= first;
if (second == 0) return first;
}
}
};
Math.LCM = function(first,second) {
return first * (second / this.GCD(first, second)); // CANNOT FIGURE OUT HOW TO EXTEND THIS TO THREE #s
};
// example
console.log(Math.GCD([4, 5, 10]));
演示也在 JSFiddle 中
注意其中关于最小公倍数 (LCM) 的函数
我正在尝试扩展此功能,以便它可以计算相同的两个或三个用户提供的输入的LCM,但我一生都无法正确处理它。我是 JavaScript 的新手,希望得到任何帮助。请注意,如果字段留空,则也应从计算中省略该字段,就像GCD所做的那样。
您可以使用以下函数:
function gcd2(a, b) {
// Greatest common divisor of 2 integers
if(!b) return b===0 ? a : NaN;
return gcd2(b, a%b);
}
function gcd(array) {
// Greatest common divisor of a list of integers
var n = 0;
for(var i=0; i<array.length; ++i)
n = gcd2(array[i], n);
return n;
}
function lcm2(a, b) {
// Least common multiple of 2 integers
return a*b / gcd2(a, b);
}
function lcm(array) {
// Least common multiple of a list of integers
var n = 1;
for(var i=0; i<array.length; ++i)
n = lcm2(array[i], n);
return n;
}
也许你稍微改变了 GCD
和 LCM
的结构,这样两个方法都只有两个参数。
要获取两个以上参数的结果,请使用 Array.prototype.reduce()
,它从数组中获取两个元素并返回一个结果,该结果用作新的插入,直到数组完成。
虽然 LCM 和 GCD 是关联的,但您可以根据需要将其链接起来。
Math.GCD = function twogcd(first, second) {
if (first < 0) first = -first;
if (second < 0) second = -second;
if (second > first) { var temp = first; first = second; second = temp; }
while (true) {
first %= second;
if (first == 0) return second;
second %= first;
if (second == 0) return first;
}
};
Math.LCM = function (first, second) {
return first * (second / Math.GCD(first, second));
};
document.getElementById('calc').addEventListener('click', function (e) {
var first = +document.getElementById("first").value,
second = +document.getElementById("second").value,
third = +document.getElementById("third").value,
numbers = [first, second, third],
resultGCD = numbers.reduce(Math.GCD), // just chain it together
resultLCM = numbers.reduce(Math.LCM); // just chain it together
document.getElementById('gcd').innerHTML = resultGCD;
document.getElementById('lcm').innerHTML = resultLCM;
});
GCD: <span id="gcd"></span><br />
LCM: <span id="lcm"></span><br />
<form name="sci-calc" method="POST" id="sci-calc">
<input type="text" name="stuff[]" class="input-field" id="first" /><br />
<input type="text" name="stuff[]" class="input-field" id="second" /><br />
<input type="text" name="stuff[]" class="input-field" id="third" /><br />
<button type="button" id="calc">CALC</button>
</form>
我还没有弄清楚你的代码在做什么,但这是查找 LCM 的函数:
LCM = function(numbers) {
console.log(numbers)
if (numbers.length < 2) return
first = numbers[0]
second = numbers[1]
var i = j = 1
var mult1 = first * i++
var mult2 = second * j++
while (mult1 != mult2) {
if (mult1 < mult2)
mult1 = first * i++
else
mult2 = second * j++
}
if (numbers.length > 2) {
numbers[1] = mult1 //I hope you're fine with the fact that 'numbers' gets modified
mult1 = LCM(numbers.splice(1, numbers.length-1))
}
return mult1
}
我知道它没有效率,但它说明了如何将它与任意数量的参数一起使用的想法(它只是递归调用)。
小提琴:https://jsfiddle.net/grabantot/fr0gzogL/
相关文章:
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 如何使用PHP和JS级联三个下拉列表
- 灯光问题(使用三个.js)
- 在三个.js中创建球体(例如地球)的磁力线
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- javascript第三个XMLHttpRequest被拒绝.但前两个是允许的
- 将循环中的两个文本框相乘,并在第三个文本框上显示结果
- 可以't将几个数字设置为<输入类型=“;数字“>
- 正好包含2个大写字母和3个数字的正则表达式
- 根据前两个下拉ID显示第三个下拉列表
- 将文本字段限制为三个数字
- regex:如何捕获字符串中的三个数字,而不是字符串的其余部分
- 我将如何创建一个函数,该函数将三个数字作为参数并返回其中最大的一个
- 在 JavaScript 中计算两个或三个数字的 LCM
- I'我试图创建一个函数,将范围(0-100)内的三个数字随机化,并打印最大的一个
- 每隔三个数字字符添加一个逗号
- 在三个数字后面加空格
- 正则表达式检查前三个字符的数字是不工作的字母
- 每三个数字加逗号或空格分组
- 用三个数字创建一个百分比