我使用以下代码来确定两个或三个数字的GCD:
$('#calc').click(function(){
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
};
var first = document.getElementById("first").value;
var second = document.getElementById("second").value;
var third = document.getElementById("third").value;
var numbers = [first,second,third];
var GCDresult = Math.GCD(numbers);
alert(GCDresult);
});
注意那里关于LCM的功能。
这是我的HTML:
<FORM NAME="sci-calc" method="POST" id="sci-calc">
<button TYPE="button" ID="calc">CALC</button>
<input type="text" name="stuff[]" class="input-field" id="first"/>
<input type="text" name="stuff[]" class="input-field" id="second"/>
<input type="text" name="stuff[]" class="input-field" id="third"/>
</FORM>
小提琴:https://jsfiddle.net/59z28rpk/
我正在尝试扩展此功能,以便它可以计算相同的两个或三个用户提供的输入的LCM,但我不能为我的生活做得对。我是JavaScript的新手,非常感谢任何帮助。请注意,如果一个字段留空,它也应该从计算中省略,就像对GCD一样。
答案 0 :(得分:1)
我还没弄清楚你的代码在做什么,但这里有找到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
}
我知道它没有效率,但它说明了如何将它与任意数量的参数一起使用的想法(它只是递归地调用)。
答案 1 :(得分:1)
您可以使用以下功能:
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;
}
答案 2 :(得分:0)
也许您为GCD
和LCM
更改了一点结构,因此两个方法只有两个参数。
要获取两个以上参数的结果,请使用Array.prototype.reduce()
,它从数组中获取两个元素并返回一个结果,该结果将用作新插入,直到数组完成。
虽然LCM和GCD为associative,但您可以根据需要对其进行链接。
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>