12,696,403 members (30,465 online)
Rate this:
See more:
```public class CubeSum implements Comparable<CubeSum> {
private final int sum;
private final int i;
private final int j;

public CubeSum(int i, int j) {
this.sum = i*i*i + j*j*j;
this.i = i;
this.j = j;
}

public int compareTo(CubeSum that) {
if (this.sum < that.sum) return -1;
if (this.sum > that.sum) return +1;
return 0;
}

public String toString() {
return sum + " = " + i + "^3" + " + " + j + "^3";
}

public static void main(String[] args) {

int N = Integer.parseInt(args[0]);

// initialize priority queue
MinPQ<CubeSum> pq = new MinPQ<CubeSum>();
for (int i = 0; i <= N; i++) {
pq.insert(new CubeSum(i, i));
}

// find smallest sum, print it out, and update
while (!pq.isEmpty()) {
CubeSum s = pq.delMin();
StdOut.println(s);
if (s.j < N) pq.insert(new CubeSum(s.i, s.j + 1));
}
}

}```

The program prints 0(0^3+0^3) 1(0^3+1^3) 2(1^3 + 1^3) 8(0^3+2^3) 9(1^3+2^3) etc when a user supplies 10 for n,but i dont understand how dis is done.i saw this code online
Posted 26-Jul-12 4:15am
brian 3461
Updated 26-Jul-12 6:34am
Joan Murt89.5K
v3
Sandeep Mewara 26-Jul-12 9:23am

I doubt on how anyone will explain you how the code is resulting in what you say.

Did DEBUGGING the code not helped?
brian 3 26-Jul-12 9:27am

it did not help..iam still confused??? plz help
lewax00 26-Jul-12 10:11am

I updated your question, this isn't C++, it's Java, and I fixed the pre tags to match.
brian 3 26-Jul-12 10:31am

iam also trying to use dis program to find all distinct integers a,b,c and d between 0 and 10^6 such dat a^3 + b^3 = c^3 + d^3 eg 1729 = 9^3 + 10^3 = 1^3 + 12^3,but iam confused on how to do it,can u help me

How is this repost different from your previous homework?
Richard MacCutchan 26-Jul-12 11:39am

It doesn't include all those comments about it being homework; so it's bound to fool all those dim witted CPians.

Kind of funny actually. So far the story is that I was assigned some homework so I searched the internet for the code instead of doing it myself. I found some random bit of code but I did not understand it so I will post it on CP to see if someone will explain it to me so when I turn it in I will be able to sound like I know what I am talking about.
Richard MacCutchan 26-Jul-12 11:59am

More sad than funny, remember these are the people who will be working in industry in a few years, writing banking and airline systems, amongst others. There certainly seems to be a modern malaise that says, "if I can't do it for myself, then it's someone else's responsibility".
Richard MacCutchan 26-Jul-12 12:00pm

Please see my comments to the original of this question. If you don't understand the mathematics of the problem then you are not going to find a code solution.
pasztorpisti 29-Jul-12 13:26pm

ROFL, the first time I see 'Homework' tag on an answer :-)

Rate this:

## Solution 1

If that code works (I've not looked at it) then you almost have it:

Main is the entry point, there are some functions, don't waste time trying to understand what a class is and try to focus on the smallest parts (divide and conquer).

Try to read it, the code itself is explanatory...

You should start looking for each instruction to see what that instruction does.

After that and little by little you will understand everything.

Good luck.
brian 3 26-Jul-12 11:57am

@joan murt....plz i have looked at it and still dont understand it...dis is not a homework assignment.. when the user enters 10 as n,it will generate all those values i wrote above.
Joan Murt 26-Jul-12 12:00pm

I'm sorry... I've seen your old post in which you were telling us that this was homework... :(
Anyway, don't expect to see it and in 2 minutes understand it... it will take longer than that.

Top Experts
Last 24hrsThis month
 OriginalGriff 308 Jochen Arndt 100 Richard Deeming 80 ppolymorphe 68 CPallini 65
 OriginalGriff 3,511 Peter Leow 2,180 Mika Wendelius 1,743 ppolymorphe 1,579 Jochen Arndt 1,414