Skip to content

Instantly share code, notes, and snippets.

@gentildpinto
Last active January 5, 2021 21:51
Show Gist options
  • Save gentildpinto/926db19f8300a5f4f5ec13ae2c10562f to your computer and use it in GitHub Desktop.
Save gentildpinto/926db19f8300a5f4f5ec13ae2c10562f to your computer and use it in GitHub Desktop.
Algorithm to add numbers recursively with ruby
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