# Is there an explicit formula for the sum of all even integers between $x$ and $y$?

Is there an explicit formula for the sum of all even integers between $$x$$ and $$y$$? I don't know about one. I'm writing a program in Python and would appreciate some help.

I know that the sum of the first $$n$$ even integers is $$n^2 + n$$.

• Yes. And it's pretty straightforward to derive. Once you know how to add the first n numbers (and why) offsetting the starting points, having them multiple of a number like all even or all the same remainder of a multiple, are straightforward to take into accout. – fleablood Feb 15 '19 at 20:22
• "I'm writing a program in python and would appreciate some help" Is the intention of the exercise to learn how to write a For loop correctly? If so, why are you asking this question here? Are you merely wanting a way to confirm your results? It should be clear whether or not the code was written correctly. – JMoravitz Feb 15 '19 at 20:30
• No, I want an explicit formula. It's not an exercise, it's for a personal thing. It will make the code look nicer – user592402 Feb 15 '19 at 20:34

Let's assume $$x$$ and $$y$$ are even, and $$y > x$$. Let $$x = 2r$$ and $$y = 2s$$, where $$r$$ and $$s$$ are integers. So $$r = x/2$$ and $$s = y/2$$, which we'll use later. Then you're looking for $$2r + 2(r+1) + \ldots + 2s$$. You have the formula for the sum of the first $$n$$ even integers, so you can write this as

$$(2 + 4 + \cdots + 2s) - (2 + 4 + \cdots + 2(r-1))$$.

Now the first thing there is the sum of the first $$s$$ even integers, and the second is the sum of the first $$r-1$$, so that's just

$$s^2 + s - ((r-1)^2 + (r-1))$$

or, simplifying a bit,

$$s^2 + s - r^2 + r.$$

Remembering that $$r = x/2$$ and $$s = y/2$$, this is

$${y^2 \over 4} + {y \over 2} - {x^2 \over 4} + {x \over 2}$$

or, if you want everything over a common denominator,

$${y^2 + 2y - x^2 + 2x \over 4}.$$

For a sanity check substitute $$x = 6, y = 12$$. Doing the sum explicitly, you get $$6+ 8 + 10 + 12 = 36$$. From this formulas, you get

$${12^2 + 2\times 12 - 6^2 + 2\times 6 \over 4} = {144 + 24 - 36 + 12 \over 4} = {144 \over 4} = 36.$$

Apply induction:

$$n^2+n+2(n+1)=(n+1)(n+2)=(n+1)^2+(n+1).$$

Correspondingly the sum of even numbers between $$2m$$ and $$2n$$ (including both) is $$n(n+1)-m (m-1).$$

Hint: $$x+(x+2)+.....+y=$$

$$k*x +(0+2+4+....+(y-x))=$$

$$k*x+2 (0+1+2+3+....)$$

Hint 2:

$$x+(x+2)+...y=$$

$$(2+4+....+y)-(2+4+... (x-2))$$

Hint 3: $$(2+4+6+......+m)+(m+......+6+4+2)=(m+2)*k$$

$$(x+(x+2)+...+y)+(y+(y-2)+.....+x)=(x+y)*k$$

.....

There's no surprises and if an idea works in one case, the same idea will work in another.