From e7868586aeffd70815dacc87c04d8cd6ec4143e3 Mon Sep 17 00:00:00 2001 From: Novojit Saha Date: Thu, 10 Oct 2019 23:35:25 +0600 Subject: [PATCH 1/2] added hcf finding algorith --- maths/find_hcf.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 maths/find_hcf.js diff --git a/maths/find_hcf.js b/maths/find_hcf.js new file mode 100644 index 0000000000..c8c45ac79f --- /dev/null +++ b/maths/find_hcf.js @@ -0,0 +1,35 @@ +/* + author: redfly1 + + More about HCF: + https://en.wikipedia.org/wiki/Greatest_common_divisor + */ + + +function findHCF(x, y) { + + // If the input numbers are less than 1 return an error message. + if (x < 1 || y < 1) { + return "Please enter values greater than zero."; + } + + // If the input numbers are not integers return an error message. + if (x != Math.round(x) || y != Math.round(y)) { + return "Please enter whole numbers."; + } + + // Now apply Euclid's algorithm to the two numbers. + while (Math.max(x, y) % Math.min(x, y) != 0) { + if (x > y) { + x %= y; + } + else { + y %= x; + } + } + + // When the while loop finishes the minimum of x and y is the HCF. + return Math.min(x, y); + + } + console.log(findHCF(27,36)); From cb79d29807862bd468619c1c481ed4fa3acabe1d Mon Sep 17 00:00:00 2001 From: vinayak Date: Wed, 6 May 2020 17:49:15 +0530 Subject: [PATCH 2/2] Update and rename find_hcf.js to FindHcf.js --- maths/FindHcf.js | 30 ++++++++++++++++++++++++++++++ maths/find_hcf.js | 35 ----------------------------------- 2 files changed, 30 insertions(+), 35 deletions(-) create mode 100644 maths/FindHcf.js delete mode 100644 maths/find_hcf.js diff --git a/maths/FindHcf.js b/maths/FindHcf.js new file mode 100644 index 0000000000..f6b696219e --- /dev/null +++ b/maths/FindHcf.js @@ -0,0 +1,30 @@ +/* + author: redfly1 + More about HCF: + https://en.wikipedia.org/wiki/Greatest_common_divisor + */ + +function findHCF (x, y) { + // If the input numbers are less than 1 return an error message. + if (x < 1 || y < 1) { + return 'Please enter values greater than zero.' + } + + // If the input numbers are not integers return an error message. + if (x !== Math.round(x) || y !== Math.round(y)) { + return 'Please enter whole numbers.' + } + + // Now apply Euclid's algorithm to the two numbers. + while (Math.max(x, y) % Math.min(x, y) !== 0) { + if (x > y) { + x %= y + } else { + y %= x + } + } + + // When the while loop finishes the minimum of x and y is the HCF. + return Math.min(x, y) +} +console.log(findHCF(27, 36)) diff --git a/maths/find_hcf.js b/maths/find_hcf.js deleted file mode 100644 index c8c45ac79f..0000000000 --- a/maths/find_hcf.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - author: redfly1 - - More about HCF: - https://en.wikipedia.org/wiki/Greatest_common_divisor - */ - - -function findHCF(x, y) { - - // If the input numbers are less than 1 return an error message. - if (x < 1 || y < 1) { - return "Please enter values greater than zero."; - } - - // If the input numbers are not integers return an error message. - if (x != Math.round(x) || y != Math.round(y)) { - return "Please enter whole numbers."; - } - - // Now apply Euclid's algorithm to the two numbers. - while (Math.max(x, y) % Math.min(x, y) != 0) { - if (x > y) { - x %= y; - } - else { - y %= x; - } - } - - // When the while loop finishes the minimum of x and y is the HCF. - return Math.min(x, y); - - } - console.log(findHCF(27,36));