Skip to content

Instantly share code, notes, and snippets.

@praveenpuglia
Created May 22, 2016 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praveenpuglia/23dd6455a24173ada7645869c7d8b25b to your computer and use it in GitHub Desktop.
Save praveenpuglia/23dd6455a24173ada7645869c7d8b25b to your computer and use it in GitHub Desktop.
function hcf(n1, n2){
var hcf = (n1 > n2) ? findHcf(n2, n1) : findHcf(n1, n2);
console.log("HCF of ", n1, " & ", n2, " is ", hcf);
}
function findHcf(b, a) {
console.log("Divisor : ", b," Dividend: ", a);
var q, r;
q = Math.floor(a/b);
r = a - b*q;
console.log("Quotient : ", q," Remainder: ", r);
if( r !== 0 ) {
console.warn("WE HAVE TO DIVIDE AGAIN");
return arguments.callee(r, b);
} else {
console.debug("REMAINDER IS ZERO. THE END OF DIVISION.");
return b;
}
}
@praveenpuglia
Copy link
Author

Wrote a Basic HCF Finder to show steps to my sister.

@hashd
Copy link

hashd commented May 23, 2016

Euclids algorithm to calculate gcd or hcf

function hcf(a, b) {
    return (a && b)? hcf(b, a % b): (a || b)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment