# time complexity of recursive sum function

I have here a function (written in python), which computes the sum of all numbers from $$a$$ to $$b$$. I'd like to know how to find mathematically it's time complexity (without using the master theorem). How can one do that?

Here's what I've tried so far:

I managed to express the left recursive side as: $$\frac{\left(2^{k}-1\right)a+b}{2^{k}}$$ (after writing the mid as a sum like so $$\frac{a+\frac{a+\frac{a+b}{2}}{2}}{2}$$ (the fraction may go further for large $$k$$-s).

comparing the resulting formula to $$a$$ as a base case for the function to stop does not yield results: $$\frac{\left(2^{k}-1\right)a+b}{2^{k}}=a\Rightarrow-a+b=0\Rightarrow a=b$$ which brings me back to the "starting point" and gives no new information about $$k$$. I'd really appreciate your help :)

An easy approach is by noticing that the two recursive calls apply to two subintervals that partition the input interval. As the recursion stops on singletons, the function will exit immediately exactly n = b-a+1 times (the number of singletons in [a,b]).

But any call involves at most two subcalls, so that the total number of calls cannot exceed

n + n/2 + n/4 + n/8 + ... < 2n.


Clearly, that fuction takes time Θ(n).

• Thank you, I have a few followups: why is the number of calls the sum of this? also, is there a more "equational" way to get this result? for example as in the proof for the time complexity of binary search here Sep 9, 2020 at 15:20
• @bendaMan: my answer is perfectly equational. "why is the number of calls the sum of this" is what my second paragraph tries to explain.
– user65203
Sep 9, 2020 at 15:23
• I see, but still why isn't my equation working? Sep 9, 2020 at 15:28
• @bendaMan: I don't think that your computation is finalized.
– user65203
Sep 9, 2020 at 15:42
• how should it be continued? Sep 9, 2020 at 15:49