0

This is my code:

def recurSum(x, y):
    if x <= 1:
        return x
    return x + recurSum(x - 1)
    if y <= 1:
        return y

xxwjhefbhwjbfwjh efjehwbf ebrfe print(recurSum(5, 10))

  • 1
    Here is your recursive rule: `sum(x,y) = x + sum(x-1, y)` for `x>1` and `sum(1, y)=y+1`. Now just implement it. – Eugene Sh. Jul 19 '21 at 21:41
  • @trincot `Sum(x, y) = y + 1+2+3+4+...+x` - this is how I understand it. – Eugene Sh. Jul 19 '21 at 21:45
  • 1
    Unless this is an exercise in learning about recursion, you should't be *using* recursion. `return y + sum(range(x+1))`. – chepner Jul 19 '21 at 21:47
  • @trincot As Eugene said my aim is to reach Sum(x, y) = y + 1+2+3+4+...+x but I messed up the (5, 10), I wasnt sure what I was doing.. –  Jul 19 '21 at 21:48
  • OK, so why not `y + recurSum(x)`, where `recurSum(x)` will just do what this function originally seem to have been doing? – trincot Jul 19 '21 at 21:49
  • @EugeneSh. can you please explain where I should put the sum (1, y) = y+1 into my code? shuld it be in the def or outside? –  Jul 19 '21 at 21:51
  • @sadasi78 This is the base case (terminating condition). Your code should have two branches - for `x > 1` and for `x==1` – Eugene Sh. Jul 19 '21 at 21:56
  • @chepner Well, one could do `y+x*(x-1)//2` and go have a coffee :) – Eugene Sh. Jul 19 '21 at 22:00
  • @EugeneSh. Depends on how you interpret the problem being assigned :) Computationally, there's a difference between "add up the numbers" and "compute the sum", even if they are mathematically the same. – chepner Jul 19 '21 at 22:01
  • You should not edit the question with a "solution". It is a question and should remain such – Eugene Sh. Jul 19 '21 at 22:15

3 Answers3

1

You can use the following:

def recurSum(x, y):
    if x==0:
        return y
    
    return recurSum(x - 1, y) + x

In essence, the base case is when the first number has reached 0. If so, you return the other number. Otherwise, you perform once again the recursive sum.

chepner
  • 497,756
  • 71
  • 530
  • 681
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • @chepner, I didn't get your suggestion, and you actually deleted it. – lmiguelvargasf Jul 19 '21 at 21:55
  • Sorry, at first glance I thought you had fixed the answer, so I deleted the comment. You need to add `x`, because the recursive sum should be adding up `y + 0 + 1 + ... + (x-1)`. Each recursive call gets a smaller value of `x`, so in the end you will add up `0 + 1 + ... + x`, not `x + x + ... x`. If you add 1, you are just adding up `1 + 1 + 1 + ... + 1`, `x` times. – chepner Jul 19 '21 at 21:56
  • @chepner, gotta. – lmiguelvargasf Jul 19 '21 at 22:09
1

This is another way to do this.

    def recurSum(x, y):
        return y if x < 1 else recurSum(x-1, x + y)

The expanded form of the above is:

    def recurSum(x, runningSum):
        if x < 1:
            return runningSum;
        else:
            return recurSum(x-1, x + runningSum)
Ahmed Saeed
  • 102
  • 1
  • 3
  • Interesting. I wonder how did you come up with the rule? – Eugene Sh. Jul 19 '21 at 22:07
  • Python does not have [Tail Call Optimization](https://stackoverflow.com/questions/310974/what-is-tail-call-optimization) but some other languages do. I wanted a solution which could leverage TCO and this is what I came up with. – Ahmed Saeed Jul 19 '21 at 22:09
  • 1
    It's tail-recursive, but it still doesn't leverage TCO, because as you said, Python doesn't *do* TCO. – chepner Jul 19 '21 at 22:11
-1

Does the function need to be recursive? I think you might be over complicating this a bit;

def sums(x,y):
    output = y + sum(range(0,x+1))
    return output

Here in one line you can add the values between 1 and x to y

ryanf
  • 1
  • 1
  • 2
    This is not recursive. – Eugene Sh. Jul 19 '21 at 21:52
  • 2
    While this is not a suitable problem for recursion in Python, it's still worth asking how the recursive solution would work. – chepner Jul 19 '21 at 21:54
  • Recursion seem like its over complicating it, so figured I'd throw out a non-recursion answer in case they were just trying to use recursion because they thought it was most suitable – ryanf Jul 19 '21 at 22:03
  • That's something to confirm in the comments, not something to just assume. – chepner Jul 19 '21 at 22:31
  • Would've if I could've commented. Wasn't assuming, just asking a question – ryanf Jul 20 '21 at 01:48