ruby - Sum integers between two numbers with a recursive method -


i want sum integers between , including min , max recursive method. example: min = 1, max= 5, sum = 1 + 2 + 3 + 4 + 5

my issue can't find how stop "loop" created recursivity:

def sum_recursive(min, max)    return min if                   #i don't know how stop    min += (min + 1)    sum_recursive(min, max) end 

i have used counter, need create variable reset original value each time function calls itself.

is there way ? or different way organize method ?

this should give correct answer:

def sum_recursive(min, max)    return min if min == max    min + sum_recursive(min + 1, max) end 

the process simple enough:

sum_recursive(1, 3)1 + sum_recursive(2, 3)1 + (2 + sum_recursive(3, 3))1 + (2 + (3))


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -