Euclidian Algorithm
A basic recursive algorithm used to find the greatest common divisor of two numbers.
by sirfizx
HTML
<input type='text' id='firstNumber' placeholder='First Number'/>
<input type='text' id='secondNumber' placeholder='Second Number'/>
</br>
<input id='btn_compute' type='submit' value='Compute GCD'/>
JavaScript
document.getElementById('btn_compute').onclick = function computeGCD(){
var fn = document.getElementById('firstNumber').value;
var sn = document.getElementById('secondNumber').value;
// test for integer input
if(fn==Math.floor(fn) && sn==Math.floor(sn)){
var b,s,q,r,gcd;
if(fn>sn){
b=fn;
s=sn;
} else if(sn>fn) {
b=sn;
s=fn;
} else alert('The greatest common divisor is '+fn+'.');
while(r!==0){
console.log('b='+b+' and s='+s);
q= Math.floor(b/s);
gcd=s;
r= b % s;
b=s;
s=r;
console.log('r='+r+' and gcd='+gcd);
}
alert('The greatest common divisor is '+gcd+'.');
}//end test for integer input
};// end onclick function