Skip to content

Instantly share code, notes, and snippets.

@HallexCosta
Created April 6, 2021 12:09
Show Gist options
  • Save HallexCosta/a6647268b2ff7d413af458953192f8d5 to your computer and use it in GitHub Desktop.
Save HallexCosta/a6647268b2ff7d413af458953192f8d5 to your computer and use it in GitHub Desktop.
Recursive division using only subtraction and addition
/*
* @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