admin管理员组文章数量:1279048
I am using the following code to determine the Greatest Common Denominator (GCD) of two or three numbers:
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]));
I am using the following code to determine the Greatest Common Denominator (GCD) of two or three numbers:
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]));
Demo also in JSFiddle
Notice the function in there about the Least Common Multiple (LCM)
I am trying to extend this function so that it can pute the LCM of the same two or three user supplied inputs, but I cannot for the life of me get it right. I am a novice at JavaScript and would appreciate any help whatsoever. Note that if a field is left blank it should be omitted from the calculation also, as is done for the GCD.
Share Improve this question edited Oct 27, 2021 at 4:34 KyleMit♦ 30.2k72 gold badges509 silver badges700 bronze badges asked Jan 22, 2016 at 18:42 newbie2015newbie2015 2513 silver badges17 bronze badges 1- @Nina: thank you ever so much! :) – David Thomas Commented Jan 22, 2016 at 20:50
3 Answers
Reset to default 8You can use these functions:
function gcd2(a, b) {
// Greatest mon divisor of 2 integers
if(!b) return b===0 ? a : NaN;
return gcd2(b, a%b);
}
function gcd(array) {
// Greatest mon 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 mon multiple of 2 integers
return a*b / gcd2(a, b);
}
function lcm(array) {
// Least mon multiple of a list of integers
var n = 1;
for(var i=0; i<array.length; ++i)
n = lcm2(array[i], n);
return n;
}
Maybe you change the structure a little bit for GCD
and LCM
, so that both methods have only two arguments.
To get the result of more than two arguments, use Array.prototype.reduce()
, which takes two elements from the array and returns a result, which is used as a new insert until the array is finished.
And while the LCM and the GCD are associative, you can chain it as desired.
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>
I haven't figured out what your code is doing but here is the function to find 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
}
I know It's not efficcient but it illustrates the idea of how to use it with any number of parameters (it's just called recursively).
The fiddle: https://jsfiddle/grabantot/fr0gzogL/
本文标签: mathCalculate the LCM of two or three numbers in JavaScriptStack Overflow
版权声明:本文标题:math - Calculate the LCM of two or three numbers in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741234208a2362680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论