Created
April 6, 2021 12:09
-
-
Save HallexCosta/a6647268b2ff7d413af458953192f8d5 to your computer and use it in GitHub Desktop.
Recursive division using only subtraction and addition
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
/* | |
* @param a int {Amount to be divided} | |
* @param b int {Number of times the @ param1 value will be divided} | |
*/ | |
function recursive_division (a, b){ | |
if (b == 0) | |
throw new Error('Cannot divide by zero') | |
else if (a < b) | |
return [0, a] | |
else | |
[x, y] = recursive_division(a - b, b) | |
return [x+1, y] | |
} | |
console.log(recursive_division(20, 2)) // output: [10, 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment