2016-01-22 87 views
0

兩個或三個數字的LCM我使用下面的代碼來確定兩個或三個數字的GCD:計算在JavaScript

$('#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

請問一個 「LCM」 和 「GCD」 是什麼,或者是誰? –

+0

@DavidThomas,最小公倍數和最大公約數。 –

+0

@妮娜:非常感謝你! :) –

回答

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; 
} 
+0

試過了,'lcm2'函數似乎返回第二個輸入值,而不是實際的LCM。 – newbie2015

+0

@ newbie2015它適用於我,例如'lcm2(6,15)=== 30',而不是'15'。 – Oriol

+0

我做了這個小提琴,改變了一些變量,以對應我所擁有的:https://jsfiddle.net/tjj7won4/85/ - 也許我錯誤地實現了一些東西。 – newbie2015

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 
} 

我知道這不是efficcient,但它說明了如何使用任意數量的使用它的想法參數(它只是遞歸地調用)。

小提琴:https://jsfiddle.net/grabantot/fr0gzogL/

+0

我真的很喜歡這個解決方案,但我更願意堅持三個輸入字段,所以我篡改了你的小提琴[https://jsfiddle.net/fr0gzogL/17/],但我無法接受它只有兩個參數。可能嗎? – newbie2015

0

也許你改變了結構的點點GCDLCM,所以這兩種方法只有兩個參數。

要獲得的兩個以上的參數的結果,使用Array.prototype.reduce(),這需要從陣列兩個元素並返回一個結果,直到該數組完成其被用作一個新的插入件。

而且,儘管LCMGCDassociative,您可以根據需要IT連鎖。

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>