Ad
Code
Diff
  • 
    def sum n
    	n == 1 ? 1 : n + sum(n - 1)
    end
    • def sum n
    • def acc(n, s)
    • return s if n <= 0 # Base case
    • acc(n - 1, s + n) # Recursion step, with accumulator
    • end
    • acc(n, 0)
    • n == 1 ? 1 : n + sum(n - 1)
    • end