Last active
September 14, 2019 09:37
-
-
Save nkgokul/fc8e03e75ff07919d76df5dea79ab75d to your computer and use it in GitHub Desktop.
Recursive Digit Sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
function recuresiveDigitSumMaths(number) { | |
finalRecuresiveDigitSum = 0; | |
while (number) { | |
digit = number % 10; | |
finalRecuresiveDigitSum += digit; | |
if (finalRecuresiveDigitSum > 9) finalRecuresiveDigitSum -= 9; | |
number = (number - digit) / 10 | |
} | |
return (finalRecuresiveDigitSum); | |
} | |
function recuresiveDigitSumMod9(number) { | |
return (number % 9); | |
} | |
function recuresiveDigitSumMaths(number) { | |
finalRecuresiveDigitSum = 0; | |
while (number) { | |
digit = number % 10; | |
finalRecuresiveDigitSum += digit; | |
if (finalRecuresiveDigitSum > 9) finalRecuresiveDigitSum -= 9; | |
number = (number - digit) / 10 | |
} | |
return (finalRecuresiveDigitSum); | |
} | |
function recuresiveDigitSumBrute(number) { | |
digitSum = 0; | |
while (number) { | |
digit = number % 10; | |
digitSum += digit; | |
number = (number - digit) / 10 | |
} | |
if (digitSum <= 9) { | |
return digitSum; | |
} | |
else { | |
return recuresiveDigitSumBrute(digitSum); | |
} | |
} | |
function runTestforFixedValues(algorithms) { | |
fixedValues = [0, 1, 10, 123, 999, 1000, 123456789]; | |
fixedValues.map(function (number) { | |
console.log('--------------------------------'); | |
console.log("Input : " + number); | |
algorithms.map(function (algorithm) { | |
console.log("Using " + algorithm.name + " " + algorithm(number));; | |
}); | |
}); | |
} | |
function runTestforRandom(algorithms) { | |
randomValues = []; | |
max = 100000000; | |
number = getRandomNumber(1, max); | |
console.log('--------------------------------'); | |
console.log("Input : " + number); | |
algorithms.map(function (algorithm) { | |
console.log("Using " + algorithm.name + " " + algorithm(number));; | |
}); | |
} | |
function runTests() { | |
algorithms = [recuresiveDigitSumBrute, recuresiveDigitSumMaths, recuresiveDigitSumMod9]; | |
console.log("==========================================="); | |
console.log("Running Tests for Fixed Values\n"); | |
runTestforFixedValues(algorithms); | |
console.log("==========================================="); | |
console.log("\nRunning Tests for Random Values\n"); | |
loop(runTestforRandom, 10, [algorithms]); | |
} | |
function getRandomNumber(start = 1, end = 10) { | |
if (start > end) { | |
[start, end] = [end, start]; | |
} | |
let range = end - start + 1; | |
return (parseInt(Math.random() * range) % range) + start; | |
} | |
const loop = (fn, times = 5, params = []) => { | |
if (!times) { | |
return; | |
} | |
fn(...params); | |
loop(fn, times - 1, params); | |
}; | |
runTests(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment