# problem with while loops

Show 80 post(s) from this thread on one page
Page 3 of 3 First 123
• 07-09-2005
MyntiFresh
Thank you all so much for your help!!! It's really been very helpful... and I finally got my entire program to work!!
• 07-09-2005
Rashakil Fol
You have a semicolon on the line if (counter % 2 != 0); which means that squareSum = squareSum + (counter * counter); falls after the if statement. It's best to always use braces.

The following doesn't really help you learn programming, but the sum of the first n odd squares can be computed efficiently with
Code:

`int sum_first_n_odd_squares = (n * (4*(n-1)*(n+1) + 3)) / 3;`
so that if n were 5, sum_first_n_odd_squares would be equal to 1*1 + 3*3 + 5*5 + 7*7 + 9*9.

Using this formula, you could write
Code:

```int x = (firstNum + 1) / 2; int y = secondNum / 2; int oddsq_sum = (y * (4*(y-1)*(y+1) + 3) - x * (4*(x-1)*(x+1) + 3)) / 3;```
I guess the moral is that instead of spending a minute writing a while loop, you could spend five minutes inventing a formula! :p
• 07-09-2005
dwks
The problem is already fixed. :)
• 07-09-2005
JoshR
Dude so much easier with a loop... And takes no time at all... Save math for the fun stuff...
• 07-09-2005
Prelude
>instead of spending a minute writing a while loop, you could spend five minutes inventing a formula! :p
We're not all math majors, you know. In fact, my knowledge of mathematics is iffy at best, and that's pretty common. So, I'd much rather spend a minute writing a loop than days figuring out how to invent a formula. :p
• 07-09-2005
major_small
I'd have to agree with prelude... some things are just soo much easier when you let the computer figure them out for you :P
Show 80 post(s) from this thread on one page
Page 3 of 3 First 123