mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
chore: Merge pull request #670 from suryapratapsinghsuryavanshi/master
Added GetGCD and ReverseNumber method in Maths category.
This commit is contained in:
32
Maths/GetEuclidGCD.js
Normal file
32
Maths/GetEuclidGCD.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
Problem statement and Explanation : https://en.wikipedia.org/wiki/Euclidean_algorithm
|
||||||
|
|
||||||
|
In this method, we have followed the iterative approach to first
|
||||||
|
find a minimum of both numbers and go to the next step.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GetEuclidGCD return the gcd of two numbers using Euclidean algorithm.
|
||||||
|
* @param {Number} arg1 first argument for gcd
|
||||||
|
* @param {Number} arg2 second argument for gcd
|
||||||
|
* @returns return a `gcd` value of both number.
|
||||||
|
*/
|
||||||
|
const GetEuclidGCD = (arg1, arg2) => {
|
||||||
|
// firstly, check that input is a number or not.
|
||||||
|
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') {
|
||||||
|
return new TypeError('Argument is not a number.')
|
||||||
|
}
|
||||||
|
// check that the input number is not a negative value.
|
||||||
|
if (arg1 < 1 || arg2 < 1) {
|
||||||
|
return new TypeError('Argument is a negative number.')
|
||||||
|
}
|
||||||
|
// Find a minimum of both numbers.
|
||||||
|
let less = arg1 > arg2 ? arg2 : arg1
|
||||||
|
// Iterate the number and find the gcd of the number using the above explanation.
|
||||||
|
for (less; less >= 2; less--) {
|
||||||
|
if ((arg1 % less === 0) && (arg2 % less === 0)) return (less)
|
||||||
|
}
|
||||||
|
return (less)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = GetEuclidGCD
|
29
Maths/ReverseNumber.js
Normal file
29
Maths/ReverseNumber.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
Problem statement and Explanation : https://medium.com/@ManBearPigCode/how-to-reverse-a-number-mathematically-97c556626ec6
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ReverseNumber return the reversed value of the given number.
|
||||||
|
* @param {Number} n any digit number.
|
||||||
|
* @returns `Number` n reverse in reverse.
|
||||||
|
*/
|
||||||
|
const ReverseNumber = (number) => {
|
||||||
|
// firstly, check that input is a number or not.
|
||||||
|
if (typeof number !== 'number') {
|
||||||
|
return new TypeError('Argument is not a number.')
|
||||||
|
}
|
||||||
|
// A variable for storing the reversed number.
|
||||||
|
let reverseNumber = 0
|
||||||
|
// Iterate the process until getting the number is 0.
|
||||||
|
while (number > 0) {
|
||||||
|
// get the last digit of the number
|
||||||
|
const lastDigit = number % 10
|
||||||
|
// add to the last digit to in reverseNumber
|
||||||
|
reverseNumber = reverseNumber * 10 + lastDigit
|
||||||
|
// reduce the actual number.
|
||||||
|
number = Math.floor(number / 10)
|
||||||
|
}
|
||||||
|
return reverseNumber
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ReverseNumber
|
Reference in New Issue
Block a user