Efficient Equation for Counting Even Numbers from 1 to n

  • Thread starter Thread starter Genericcoder
  • Start date Start date
  • Tags Tags
    Count even
Genericcoder
Messages
130
Reaction score
0
Hi guys,

I am noobie in number theory so if something exists better than this equation I did please don't bash me this is my first equation.

Today I was thinking of a way to count all even from 1 to n,so this way I thought about is like this.

Let me first write the equation,then I will explain the logic.


iNumber = ((iNumber + 1) * (iNumber / 2)) - (((iNumber + 1) * (iNumber / 2) - (iNumber / 2)) / 2)


Okay let me explain how I got this logic,so let's pick odd numbers from 1 to 10 and see how they form.
1 = 0 + 1;
3 = 2 + 1;
5 = 4 + 1;
7 = 6 + 1;
9 = 8 + 1;

So here we have 5 ones,so for example if we removed those ones from the a equation that you add 1 to 10 from it will be like this;

Normal numbers which you added 1 to 10 ->

1 + 2 + 2 + 1 + 4 + 4 + 1 + 6 + 6 + 1 + 8 + 8 + 1 + 10 -
1 + 1 + 1 + 1 + 1,so the numbers now become

0 + 2 + 2 + 4 + 4 + 6 + 6 + 8 + 8 + 10 = 50,but that's 2x,so if we got that number and divide by 2 it will be 25,thats how much odds add up,so if we got the total numbers from 1 to 10 and minus 25 we should get the even number we want;

So let's put that in the equation and see if its right;

((11) * (5)) - (((11) * (5) - (5)) / 2) = 55 - 25 = 30;

2 + 4 + 6 + 8 + 10 = 30;

So the equation is right I am sure this has been done by another mathmetician,but its good to think about it the logic of it is great.
 
Physics news on Phys.org
Hi, genericcoder,
I'll try to illustrate how your formula connects with other formulas known to mathematicians; in other words, how to prove (and simplify) your formula, if this is of any use to you.

A formula well known by mathematicians (and possibly by you) is a formula for the sum of the first consecutive integers; for example, 1 + 2 + 3 + 4 + 5. The formula is\frac {k (k+1)} 2In this example, 1 + 2 + 3 + 4 + 5 = 5 * 6 / 2 = 30 / 2 = 15.

You want to calculate the sum of consecutive even numbers, such as 2 + 4 + 6 + 8 + 10, and that is twice the sum 1 + 2 + 3 + 4 + 5. So, if your number is 10, you would apply the above formula with 10/2 = 5 (which gives you 1+2+3+4+5; we did that and obtained 5*6/2 = 15) and then multiply the result by two to obtain 2+4+6+8+10 (= 15*2 = 30).

So the sum of the first even numbers can be expressed as twice the above formula, that is, k(k+1), but using a value of \displaystyle {k=\frac n 2}:\begin{align*}\frac n 2 \left( \frac n 2 + 1 \right) &= \frac {n^2} 4 + \frac n 2 = \frac{n^2 + 2n} 4 \\ &= \frac {n(n+2)} 4 & \mbox{(eq. 1)}\end{align*}
In your example, if you evaluate \displaystyle {\frac {n(n+2)} 4} with n=10, you obtain 10*12/4 = 30, as you did.

The formula you give can be simplified to this one marked as (eq. 1), with a little algebra. Your formula is\displaystyle {(n+1)\frac n 2 - \frac {(n+1)\frac n 2 - \frac n 2} 2}
Notice that the numerator of the big fraction on the right is \displaystyle (n+1)\frac n 2 - \frac n 2; in other words, (n+1)*something - something. If you have (n+1) of something, and subtract one of something, you are left with n*something. So we begin by simplifying your formula to\displaystyle {(n+1)\frac n 2 - \frac {n\frac n 2} 2}
or(n+1)\frac n 2 - \frac {n^2} 4
Expanding the parentheses on the left part, we obtain\begin{align*}& \frac {n^2} 2 + \frac n 2 - \frac {n^2} 4 \\ &= \frac {n^2} 4 + \frac n 2 \\ &= \frac {n(n+2)} 4 \end{align*}
just as we did above for (eq. 1).

Hope this helps!
 
@Dodo

Thanks a lot that makes sense,also this logic for calculating the even is more efficient than mine I made it more complex.
 
Thread 'Derivation of equations of stress tensor transformation'
Hello ! I derived equations of stress tensor 2D transformation. Some details: I have plane ABCD in two cases (see top on the pic) and I know tensor components for case 1 only. Only plane ABCD rotate in two cases (top of the picture) but not coordinate system. Coordinate system rotates only on the bottom of picture. I want to obtain expression that connects tensor for case 1 and tensor for case 2. My attempt: Are these equations correct? Is there more easier expression for stress tensor...
Back
Top