File tree Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Problem statement and Explanation : https://en.wikipedia.org/wiki/Euclidean_algorithm
3
+
4
+ In this method, we have followed the iterative approach to first
5
+ find a minimum of both numbers and go to the next step.
6
+ */
7
+
8
+ /**
9
+ * GetEuclidGCD return the gcd of two numbers using Euclidean algorithm.
10
+ * @param {Number } arg1 first argument for gcd
11
+ * @param {Number } arg2 second argument for gcd
12
+ * @returns return a `gcd` value of both number.
13
+ */
14
+ const GetEuclidGCD = ( arg1 , arg2 ) => {
15
+ // firstly, check that input is a number or not.
16
+ if ( typeof arg1 !== 'number' || typeof arg2 !== 'number' ) {
17
+ return new TypeError ( 'Argument is not a number.' )
18
+ }
19
+ // check that the input number is not a negative value.
20
+ if ( arg1 < 1 || arg2 < 1 ) {
21
+ return new TypeError ( 'Argument is a negative number.' )
22
+ }
23
+ // Find a minimum of both numbers.
24
+ let less = arg1 > arg2 ? arg2 : arg1
25
+ // Iterate the number and find the gcd of the number using the above explanation.
26
+ for ( less ; less >= 2 ; less -- ) {
27
+ if ( ( arg1 % less === 0 ) && ( arg2 % less === 0 ) ) return ( less )
28
+ }
29
+ return ( less )
30
+ }
31
+
32
+ module . exports = GetEuclidGCD
Original file line number Diff line number Diff line change
1
+ /*
2
+ Problem statement and Explanation : https://medium.com/@ManBearPigCode/how-to-reverse-a-number-mathematically-97c556626ec6
3
+ */
4
+
5
+ /**
6
+ * ReverseNumber return the reversed value of the given number.
7
+ * @param {Number } n any digit number.
8
+ * @returns `Number` n reverse in reverse.
9
+ */
10
+ const ReverseNumber = ( number ) => {
11
+ // firstly, check that input is a number or not.
12
+ if ( typeof number !== 'number' ) {
13
+ return new TypeError ( 'Argument is not a number.' )
14
+ }
15
+ // A variable for storing the reversed number.
16
+ let reverseNumber = 0
17
+ // Iterate the process until getting the number is 0.
18
+ while ( number > 0 ) {
19
+ // get the last digit of the number
20
+ const lastDigit = number % 10
21
+ // add to the last digit to in reverseNumber
22
+ reverseNumber = reverseNumber * 10 + lastDigit
23
+ // reduce the actual number.
24
+ number = Math . floor ( number / 10 )
25
+ }
26
+ return reverseNumber
27
+ }
28
+
29
+ module . exports = ReverseNumber
You can’t perform that action at this time.
0 commit comments