Tail_recursion.rb
This content was produced by an LLM and could include errors.
This script defines a method that calculates the sum of all integers from 1 up to the given number. Executing sum(10) correctly outputs the sum of 1 through 10, which is 55.
# frozen_string_literal: true
#=> nil
def sum(n)
m = 0
while n != 1
m += n
n -= 1
end
m + 1
end
#=> :sum
puts sum(10)
55
#=> nil
Ruby 4.0.3