Created
May 22, 2016 11:26
-
-
Save praveenpuglia/23dd6455a24173ada7645869c7d8b25b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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
Wrote a Basic HCF Finder to show steps to my sister.