TestSum
i don't follow this code, when i run it the first couple of values of the left variable are 1 and 9.
why aren't they 0 and 8? can anyone help?
left = 0*1*1*1 = 0
left = 1*2*2*2 = 8
class TestSum{
publicstaticvoid main(String[] args){
int n = 50;
System.out.println("n = "+n);
int left = 0;
for(int i = 1; i<=n; i++){
System.out.print("left ");
System.out.println(left += i*i*i);
}
int right = n*n*(n+1)*(n+1)/4;
System.out.println(left+" = "+right);
}
}
n = 50
left 1
left 9
left 36
left 100
left 225
left 441
left 784
left 1296
left 2025
left 3025
left 4356
left 6084
left 8281
left 11025
left 14400
left 18496
left 23409
left 29241
left 36100
left 44100
left 53361
left 64009
left 76176
left 90000
left 105625
left 123201
left 142884
left 164836
left 189225
left 216225
left 246016
left 278784
left 314721
left 354025
left 396900
left 443556
left 494209
left 549081
left 608400
left 672400
left 741321
left 815409
left 894916
left 980100
left 1071225
left 1168561
left 1272384
left 1382976
left 1500625
left 1625625
1625625 = 1625625
**** JDK Commander: E n do fj a v ae x e c u t i o n****
[2051 byte] By [
mark_8206a] at [2007-11-27 2:28:10]

> > System.out.println(left += i*i*i);You are incrementing left before performing the calculation, as I understand it. So on your first loop, left is not 0, it is 1.
> i don't follow this code, when i run it the first couple of values of the
> left variable are 1 and 9.
>
> why aren't they 0 and 8? can anyone help?
>
> left = 0*1*1*1 = 0
> left = 1*2*2*2 = 8
That's not what your code does. What it does is the following:left = 0
left = left + 1*1*1 = 0 + 1*1*1 = 1
left = left + 2*2*2 = 1 + 2*2*2 = 9
left = left + 3*3*3 = 9 + 3*3*3 = 36
...
Isn't it what you want ?
The first time this is executedSystem.out.println(left += i*i*i);left = 0 and i = 1, so left += i*i*i is left = 0 + (1 * 1 * 1) = 1.Note that you did not do *=, but +=.
> The first time this is
> executedSystem.out.println(left +=
> i*i*i);
left = 0 and i = 1, so
> left += i*i*i is left = 0 + (1 * 1 * 1) =
> 1.
> Note that you did not do *=, but +=.
Oh, missed that completely, I thought += was as assignment... D'oh.
> > The first time this is
> > executedSystem.out.println(left +=
> > i*i*i);
left = 0 and i = 1, so
> > left += i*i*i is left = 0 + (1 * 1 * 1)
> =
> > 1.
> > Note that you did not do *=, but +=.
>
> Oh, missed that completely, I thought += was as
> assignment... D'oh.
it is an assignment. it just adds, too
Cheers Georgemc - I get it now... I'll never get a Java Development job at this rate... What I wouldn't give for a company that use Ingres and are willing to pay for training in Java...
yeah, thanks for the replies, i now understand the left variable.but what's going on with the number 1625625?
> yeah, thanks for the replies, i now understand the left variable.> but what's going on with the number 1625625?What do you mean?
> > yeah, thanks for the replies, i now understand the
> left variable.
> > but what's going on with the number 1625625?
>
> What do you mean?
prome, i'm talking about the last couple of ouput lines
left 1625625
1625625 = 1625625
but i'll probably be able to work it out for myself.
> > What do you mean?
>
> prome, i'm talking about the last couple of ouput
> lines
>
> left 1625625
> 1625625 = 1625625
Yes, I figured that much. You however, did not provide enough details for me to respond properly. Were/are you surprised by the output? If so, try running this:
class TestSum {
public static void main(String[] args) {
int n = 50;
System.out.println("n = "+n);
int left = 0;
for(int i = 1; i<=n; i++) {
System.out.print("left == "+left+" + ("+i+"*"+i+"*"+i+") == ");
left += i*i*i;
System.out.println(left+", right == ("+i+"*"+i+"*("+i+
"+1)*("+i+"+1)/4) == "+(i*i*(i+1)*(i+1)/4));
}
int right = n*n*(n+1)*(n+1)/4;
System.out.println(left+" = "+right);
}
}
Message was edited by:
prometheuzz
Aniother one for you: make the sum of 1, 2, 3, ... , n and then multiply it with itself:
(1+2+3+...+50)^2 == 1625625
; )