Efficient Equation for Counting Even Numbers from 1 to n

  • Context: Undergrad 
  • Thread starter Thread starter Genericcoder
  • Start date Start date
  • Tags Tags
    Count even
Click For Summary
SUMMARY

The forum discussion centers on an efficient equation for counting even numbers from 1 to n, proposed by a user named genericcoder. The equation is expressed as iNumber = ((iNumber + 1) * (iNumber / 2)) - (((iNumber + 1) * (iNumber / 2) - (iNumber / 2)) / 2). Another user illustrates how this formula relates to the well-known formula for the sum of consecutive integers, simplifying it to (n(n + 2)) / 4. This simplification demonstrates that the proposed equation can be derived from established mathematical principles, confirming its validity and efficiency.

PREREQUISITES
  • Understanding of basic algebraic manipulation
  • Familiarity with number theory concepts
  • Knowledge of formulas for the sum of consecutive integers
  • Ability to interpret mathematical equations
NEXT STEPS
  • Study the derivation of the formula for the sum of the first n integers
  • Learn about mathematical induction and its applications in number theory
  • Explore advanced techniques in algebraic simplification
  • Investigate other mathematical formulas for counting sequences
USEFUL FOR

Mathematicians, educators, students of number theory, and anyone interested in efficient mathematical computations and algebraic simplifications.

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[tex]\frac {k (k+1)} 2[/tex]In 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, [itex]k(k+1)[/itex], but using a value of [itex]\displaystyle {k=\frac n 2}[/itex]:[tex]\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*}[/tex]
In your example, if you evaluate [itex]\displaystyle {\frac {n(n+2)} 4}[/itex] with [itex]n=10[/itex], 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[tex]\displaystyle {(n+1)\frac n 2 - \frac {(n+1)\frac n 2 - \frac n 2} 2}[/tex]
Notice that the numerator of the big fraction on the right is [itex]\displaystyle (n+1)\frac n 2 - \frac n 2[/itex]; 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[tex]\displaystyle {(n+1)\frac n 2 - \frac {n\frac n 2} 2}[/tex]
or[tex](n+1)\frac n 2 - \frac {n^2} 4[/tex]
Expanding the parentheses on the left part, we obtain[tex]\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*}[/tex]
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K