Last active
January 5, 2021 21:51
-
-
Save gentildpinto/926db19f8300a5f4f5ec13ae2c10562f to your computer and use it in GitHub Desktop.
Algorithm to add numbers recursively with ruby
This file contains 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
def sum(*numbers) | |
return numbers[0] if numbers.length == 1 | |
sum_accumulator = numbers.pop | |
return sum_accumulator + sum(*numbers) | |
end | |
sum(1, 2, 3, 4) # 1 + 2 + 3 + 4 = 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment