This calculator can quickly determine the Greatest Common Factor and Least Common Multiplier between two or three numbers.
- Copy the coding into the HEAD of your HTML document
- Add the last code into the BODY of your HTML document
<script type=”text/javascript”>
<!– This script and many more are available free online at –>
<!– The JavaScript Source!! http://javascriptsource.com –>
<!– Original: Seamus Yim –>
<!– Begin
function go(input1,input2,input3){
if (document.form1.text3.value!=””){
document.form1.answer.value=hcf(hcf(input1,input2),input3);
document.form1.answer2.value=lcm(lcm(input1,input2),input3);
} else {
document.form1.answer.value=hcf(input1,input2);
document.form1.answer2.value=lcm(input1,input2);
}
}
function hcf(text1,text2){
var gcd=1;
if (text1>text2) {text1=text1+text2; text2=text1-text2; text1=text1-text2;}
if ((text2==(Math.round(text2/text1))*text1)) {gcd=text1}else {
for (var i = Math.round(text1/2) ; i > 1; i=i-1) {
if ((text1==(Math.round(text1/i))*i))
if ((text2==(Math.round(text2/i))*i)) {gcd=i; i=-1;}
}
}
return gcd;
}
function lcm(t1,t2){
var cm=1;
var f=hcf(t1,t2);
cm=t1*t2/f;
return cm;
}
// End –>
</script>
<div align=”center”>
<form name=”form1″>
First Number: <input type=”text” name=”text1″ size=”3″>
Second Number: <input type=”text” name=”text2″ size=”3″><br>
Third Number (<em>not required</em>): <input type=”text” name=”text3″ size=”3″>
<br><br>
<input type=”button” value=”Find GCF and LCM” onclick=”go(eval(document.form1.text1.value),eval(document.form1.text2.value),eval(document.form1.text3.value))”>
<br><br>
Greatest Common Factor (GCF): <input type=”text” name=”answer” size=”3″ readonly>
<br>
Least Common Multiplier (LCM): <input type=”text” name=”answer2″ size=”3″ readonly>
</form>
</div>
<p><center>
<font face=”arial, helvetica” size”-2″>Free JavaScripts provided<br>
by <a href=”https://javascriptsource.com”>The JavaScript Source</a></font>
</center><p>