We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ed4b295 commit d914bd9Copy full SHA for d914bd9
maths/FindHcf.js
@@ -0,0 +1,30 @@
1
+/*
2
+ author: redfly1
3
+ More about HCF:
4
+ https://en.wikipedia.org/wiki/Greatest_common_divisor
5
+ */
6
+
7
+function findHCF (x, y) {
8
+ // If the input numbers are less than 1 return an error message.
9
+ if (x < 1 || y < 1) {
10
+ return 'Please enter values greater than zero.'
11
+ }
12
13
+ // If the input numbers are not integers return an error message.
14
+ if (x !== Math.round(x) || y !== Math.round(y)) {
15
+ return 'Please enter whole numbers.'
16
17
18
+ // Now apply Euclid's algorithm to the two numbers.
19
+ while (Math.max(x, y) % Math.min(x, y) !== 0) {
20
+ if (x > y) {
21
+ x %= y
22
+ } else {
23
+ y %= x
24
25
26
27
+ // When the while loop finishes the minimum of x and y is the HCF.
28
+ return Math.min(x, y)
29
+}
30
+console.log(findHCF(27, 36))
0 commit comments