Skip to content

Commit 0f69f65

Browse files
authored
chore: Merge pull request TheAlgorithms#670 from suryapratapsinghsuryavanshi/master
Added GetGCD and ReverseNumber method in Maths category.
2 parents 0853f44 + 09ce0c7 commit 0f69f65

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Maths/GetEuclidGCD.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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

Maths/ReverseNumber.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy