# Thread: Sum of all even numbers up to but not including 100

1. Member
Join Date
Feb 2010
Posts
22
Rep Power
0
Originally Posted by JosAH
The ellipses (...) are a (more or less) official notation for a continuation, so the sum of all even numbers starting at 2 and ending at 98 is written as:

2+4+6+ ... +(2*49).

The sum of the first 'n' numbers (this is the general case) is written as:

1+2+3+ ... +n.

Centuries (milleniums actually) ago it was discovered that this sum equals:

n*(n+1)/2

Try some small examples. So if the sum of the first 'n' numbers can be written as n*(n+1)/2 then twice this sum can be written as n*(n+1).

So, the sum of the first even numbers up to 98 is:

2+4+6+8+ ... +2*49 == 49*(49+1) == 49*50 == 2450.

Comprendo?

kind regards,

Jos
Why do you give an example of 1+2+3...? Why even bring it up?When you say.. "THIS SUM" what sum are you talking about. When you say first n numbers WHAT NUMBERS specifically? Twice the sum? Twice the sum of what?

2. Member
Join Date
Feb 2010
Posts
22
Rep Power
0

## Loops

I just think your wrong. I can not find anyway for it to print every number without ..."stupid loops"

3. Member
Join Date
Feb 2010
Posts
22
Rep Power
0
int sum = 0;
for(int n = 0; n <= 98; n=n+2) //loops 49 times
{
sum = sum + n;
System.out.print(n + " ");
}
System.out.print(sum);
}

4. Originally Posted by bigpips305
Mathematics 101 isn't your favourite thing is it? ;-)

kind regards,

Jos

5. Originally Posted by bigpips305
I just think your wrong. I can not find anyway for it to print every number without ..."stupid loops"
Sure, you'll need a loop to print out every single number; you won't need any loops if you want to calculate the sum of all those numbers or just the even numbers, but you just won't understand my proofs or examples.In Java it would have been (warning: complete spoilers ahead)

Java Code:
```int sum1ton(int n) { return n*(n+1)/2; }
int sumeven2ton(int n) {n/=2; return n*(n+1); }```
The first little method returns the value 1+2+3+...+n while the second little method returns 2+4+8+...+n (inclusive).

kind regards,

Jos
Last edited by JosAH; 02-04-2010 at 11:30 AM.

6. Member
Join Date
Feb 2010
Posts
22
Rep Power
0

## Okay?

Okay??

For the 100th time, I dont know where you getting this 1+2+3 thing. I never even needed that. So your attempt to explain with an incomplete, fragmented sentence was just throwing me off.

And I thought it was pretty clear early on that I DID need it to print all the "EVEN" numbers As WELL as the total.

I tried cutting and pasting each of your methods in.
This is what I get:

Java Code:
```run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot return a value from method whose result type is void
at ass42.Main.main(Main.java:18)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)```
"cannot return a value from method whos result type is void"

This is what I have based on the other posters, clear and consice instructions that I actually learned somethign from, and is perfect minus the += at the end. I would like to get rid of the last plus. I learn better from example, not rabbit trails. I am deffinite your motive was sincere for me to figure it out on my own, but all you really did was make it difficult.

Java Code:
```int sum = 0;
for(int n = 0; n <= 98; n=n+2) //loops 49 times
{
sum = sum + n;
System.out.print(n + "+"); //Prints each even number and an addition sign

}
System.out.println("= " + sum); //Prints final sum
}```

7. Originally Posted by bigpips305
Okay??

For the 100th time, I dont know where you getting this 1+2+3 thing. I never even needed that.
Rejecting everything you have never needed before is a very dangerous attitude. I'm out of this thread, you do it your way and please throw my two little methods away because you don't know how to use them and you never have needed them before.

kind regards,

Jos

8. Member
Join Date
Feb 2010
Posts
22
Rep Power
0

## Rejection

I only rejected that because you were being intentially persistent on including that when I was having an obviously tough time with just the one.

I dont know, it seems like your declaring a method within the main method here.. Im not even on methods.. I just need a simple loop. If your "COMPLETE spoiler" was what I was eventually suppose to acheive from your teaching style, Im not sure I would have ever got that. So feel free to not feel like you hindered me in anyway by showing off that you had some way of solving this equation in a method or without using a loop.

And even if I did understand how to use these methods, they are wrong. They dont print the even numbers.

9. Member
Join Date
Jan 2010
Posts
26
Rep Power
0
..........
Last edited by JavaJuJitZu; 02-04-2010 at 12:30 PM. Reason: Only read first page of thread, so reply was irrelevant

10. Originally Posted by bigpips305
I only rejected that because you were being intentially persistent on including that when I was having an obviously tough time with just the one.

I dont know, it seems like your declaring a method within the main method here.. Im not even on methods.. I just need a simple loop. If your "COMPLETE spoiler" was what I was eventually suppose to acheive from your teaching style, Im not sure I would have ever got that. So feel free to not feel like you hindered me in anyway by showing off that you had some way of solving this equation in a method or without using a loop.

And even if I did understand how to use these methods, they are wrong. They dont print the even numbers.
I am not a teacher, I simply volunteer here but my impression is that if you don't understand something you simply claim that the other person was wrong; I wasn't wrong but I'm not proud of it because it is all very old and solid knowedge, I simply studied it years ago. You were your own hindrance because of your stubborn attitude. Don't do that anymore because it'll kick back in the (near) future and it most certainly doesn't help when you want to learn how to program.

I just gave you two simple methods that can calculate the end result without a loop; you didn't understand what I was talking about but simply copied and pasted the code without knowing what you were doing. Nothing wrong with that but your conclusion that I was wrong is incorrect.

Jos

11. Member
Join Date
Feb 2010
Posts
22
Rep Power
0

## Thanks

In conclusion..

The ellipses (...) are a (more or less) official notation for a continuation, so the sum of all even numbers starting at 2 and ending at 98 is written as:

2+4+6+ ... +(2*49). <--Where did you get the 49 from. Where would I have gotten that from just reading instructions to write a program in Java that finds all even numbers between 0 and 98.

The sum of the first 'n' numbers (this is the general case) is written as:

1+2+3+ ... +n. <-- what is n here? You are adding n. Is n 1?

Centuries (milleniums actually) ago it was discovered that this sum equals:

n*(n+1)/2 <--what is "this sum" the sum from the previous example?

Try some small examples. So if the sum of the first 'n' numbers can be written as n*(n+1)/2 then twice this sum can be written as n*(n+1).

I cant try a small example if I dont understand the 3 previous sentences.

So, the sum of the first even numbers up to 98 is: <--the FIRST even numbers??? isnt it all the even numbers???

2+4+6+8+ ... +2*49 == 49*(49+1) == 49*50 == 2450. <-- is this 3 different things. I dont get this.

Comprendo?

12. Member
Join Date
Feb 2010
Posts
22
Rep Power
0
Ill ask a favor.. For the sake of this assignment, since I have already a working version now. Can you please start from the top. Explain to me the logic, and then show me each step in code... Explain some logic, then show more code, ultimatly the whole thing.

So if your saying 2+4+ n = 49 or whatever
the code is :

etc... Maybe I will be able to see how you were trying to explain to me so I can actually learn what I was doing wrong. If I didnt want to believe that you were right, I wouldnt even bother.. I just need it explained more complete and with example step by step. PLEASE

looking back.. I dont see how I would have ever stumbled upon the method way like your example from the information you gave. All you did was tell me the math. How do I write the syntax. what is the proper way to write that
Last edited by bigpips305; 02-04-2010 at 12:56 PM.

13. Originally Posted by bigpips305
Ill ask a favor.. For the sake of this assignment, since I have already a working version now. Can you please start from the top. Explain to me the logic, and then show me each step in code... Explain some logic, then show more code, ultimatly the whole thing.

So if your saying 2+4+ n = 49 or whatever
the code is :

etc... Maybe I will be able to see how you were trying to explain to me so I can actually learn what I was doing wrong. If I didnt want to believe that you were right, I wouldnt even bother.. I just need it explained more complete and with example step by step. PLEASE
I'll try to explain how the ancient Greeks figured this all out and I'll use a simple example: suppose you want to add the numbers 1+2+3+4+5+6+7. You can do it the simple way and do six additions but you can also do this: take enough pebbles and arrange them like this:

Java Code:
```*
**
***
****
*****
******
*******```
Every '*' represents a pebble. Now I'l add just as many pebbles and use the following arangement:

Java Code:
```#######
*######
**#####
***####
****###
*****##
******#
*******```
Both the '*'s and '#'s represent pebbles. There are 7x8 pebbles layed out in a rectangular shape; half of it is (7x8)/2 which is the sum of 1+2+3+4+5+6+7.
So if I have to add those seven numbers I could've done: 7*(7+1)/2.

Concluding, for every integer 'n' >= 0 the sum of all integers up to this value 'n' (whatever it is) is n*(n+1)/2.

kind regards,

Jos

14. Member
Join Date
Feb 2010
Posts
22
Rep Power
0
okay.. i follow you on this..

Now even numbers?

15. Originally Posted by bigpips305
okay.. i follow you on this..

Now even numbers?
Now you understand that sum of the numbers 1+2+3+ ... +n equals n*(n+1)/2. Multiply the numbers in that sum and that formula by two; you get:

2*1+2*2+2*3+ ... +(2*n) equals 2+4+6+ ...+(2*n) equals n*(n+1)

(try a few examples, e.g. n=5: 2+4+6+8+10 == 5*6). So if you have to add all the even number up to 98, the value of 'n' is 49 (see above). The sum of all those values is 49*50 which equals 2450. No loops needed.

kind regards,

Jos

16. Member
Join Date
Feb 2010
Posts
22
Rep Power
0

## Whoa

Now you understand that sum of the numbers 1+2+3+ ... +n equals n*(n+1)/2. Multiply the numbers in that sum and that formula by two; you get:

2*1+2*2+2*3+ ... +(2*n) equals 2+4+6+ ...+(2*n) equals n*(n+1)

(try a few examples, e.g. n=5: 2+4+6+8+10 == 5*6). So if you have to add all the even number up to 98, the value of 'n' is 49 (see above). The sum of all those values is 49*50 which equals 2450. No loops needed.

n=8 2+4+6+8+10+12+14+16 == 8*9

I take the last number.. Divide it by 2. Add one to it and multiply them together and I get the answer. So i understand these concepts. I saw the pebble example and quite a few other examples at

Techniques for adding the numbers 1 to 100 | BetterExplained a few days ago...

Now... This is how I think of it at first when I am trying to find a way to solve this using computation, specifically Java.

Take the higher integer and divide it by two:

int number = 98;
int n = number / 2;

System.out.print(n*n+1)

Okay.. I have an answer.. But how do I think about how I need to print all the even numbers in Java before the answer.

17. Member
Join Date
Feb 2010
Posts
22
Rep Power
0

## Side note

On another note.. I thought it was interesting that I just bumped into this:

Comparing 3 Integers and Displaying the Largest and smallest - Java answers

What happened to your ethics ;)

18. Originally Posted by bigpips305
Take the higher integer and divide it by two:

int number = 98;
int n = number / 2;

System.out.print(n*n+1)
No, you forgot the parentheses:

Java Code:
`System.out.println(n*(n+1));`
kind regards,

Jos

19. Member
Join Date
Feb 2010
Posts
22
Rep Power
0
Right... so I mean I had this on the first day.. How does this tie in with your method. How do I use the method you suggested, and how does it fit with this?

20. Originally Posted by bigpips305
Right... so I mean I had this on the first day.. How does this tie in with your method. How do I use the method you suggested, and how does it fit with this?
I told you all this on the first day but you didn't understand it.

kind regards,

Jos

Page 2 of 3 First 123 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•