# Sum of odd ints 0-n

• 12-06-2010, 07:03 AM
falkon114
Sum of odd ints 0-n
I'm trying to write a program for my class that computes the sum off all odd integers between 0 and n, a user defined arbitrary variable.

Everything should be ok except these blocks, which is where I'm guessing I'm having the issues...
... global variables
Code:

```int n = 0; int result = 0; int newnum = 0; int count = n; //... computational code while(count>=0) {       if(n % 2 ==1)  {              //if n is odd         result = n + newnum;    //result updated         newnum = n;                //newnum equal to new odd int n         n=n - 2;                        //n set to next odd number down         count = count - 1;          //count updated       }       Else if(n % 2 == 0) {    //if n is even         n = n - 1;                    //make n odd         count = count - 1;        //continue subtracting counter       } } system.out.println(result) system.exit(0)```
//
So far, it does the first step of the program correctly as far as I can tell. If n is set to 5, it displays 8 (5+3). if n = 7, it displays 12 (7+5)
Why doesn't it continue?
• 12-06-2010, 07:18 AM
Singing Boyo
What do you actually get when you run the program? Why the count variable? Wouldn't using n>=0 work better for the while loop? You seem to have a lot of unnecessary variables in your code.
• 12-06-2010, 07:22 AM
gcalvin
Since count is the variable that will change each time through the loop, count is the variable you should check for evenness or oddness, and count is the variable you should add to result if it's odd. If it's even, you should do nothing and go on to the next iteration of the loop. I don't see any purpose for newnum.

-Gary-
• 12-06-2010, 07:27 AM
gcalvin
By the way, if you're allowed to know anything about even and odd numbers when doing this exercise, you might profit by knowing that the odd integers start at 1 and go up by twos. Are you allowed to use a for loop?

-Gary-
• 12-06-2010, 07:52 AM
jomypgeorge
int count = n;
int result = 0;
if ((count%2) ==0)
count --;

while (count > 0)
{
result = result+count;
count = count -2;
}
system.out.println(result)

is it useful?
• 12-06-2010, 08:40 AM
falkon114
Code:

```... int n = 0; int result = 0; ... while(n>=0) {         if(n%2==1) {         result = result + n;         n = n-2;       }         else {         n=n-1; } System.out.println(result);```
• 12-06-2010, 09:24 AM
JosAH
Quote:

Originally Posted by falkon114
I'm trying to write a program for my class that computes the sum off all odd integers between 0 and n, a user defined arbitrary variable.

The solutions I've seen here are all so 'computerese'; the old Greek didn't have computers but they were smarter than we are nowadays; they could think and the only tools they had were pebbles in the sand. They did this:

Code:

```     *                <-- one pebble          *  *            <-- three more pebbles          *  *               *  *  *        <-- five more pebbles              *  *  *               *  *  *                   *  *  *  *    <-- seven more pebbles                  *  *  *  *                   *  *  *  *                   *  *  *  *```
If you add nine more pebbles you get another square shape. Suppose there are P(n) pebbles in a square; if you add n+1 more pebbles you have P(n+1) == P(n)+n+1 pebbles. The length of a side of a square with P(n) pebbles is (n+1)/2. So P(n)= (n+1)*(n+1)/4. So given n (odd), the sum of all the numbers 1+3+5+6+7+ ... == (n+1)*(n+1)/4. We don't need no steenkin' loops.

kind regards,

Jos
• 12-06-2010, 09:36 AM
falkon114
Thanks for that pointer. I've been trying to think of an "equation" to represent it. I figured there had to be one, but.. Im not good with math =/
(yet)
rewrote my code, I realised my made it way too complex the first go around. here tis:
Code:

```while(n>0) {         if(n%2==1) {                 result = result + n;                 n=n-2;         }         else {                 n=n-1;         }         } System.out.println(result); } }```