Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
This program CubeSum.java that prints out all integers of the form a3 + b3 where a and b are integers between 0 and N in sorted order, without using excessive space.

That is, instead of computing an array of the N2 sums and sorting them, build a minimum-oriented priority queue, initially containing (03, 0, 0), (13, 1, 0), (23, 2, 0), ..., (N3, N, 0).

Then, while the priority queue is nonempty, remove the smallest item (i3 + j3, i, j), print it, and then, if j < N, insert the item (i3 + (j+1)3, i, j+1).


C#
public class CubeSum implements Comparable<CubeSum> {
    private final int sum;
    private final int sum2;
    private final int i;
    private final int j;
    private final int a;
    private final int b;

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

    }
public CubeSum(int w

    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>();
        pq.insert(new CubeSum(0,1);
         
        for (int i = 0,j = 1; i & j <= N; i+=2,j+=2) {
            pq.insert(new CubeSum(i, i,j,j,));
            if(i>0 && j>1)
{
            pq.insert(new CubeSum(0,i,0,j);
}
}
for(int i =0;i<N;i++)
{
for(int j=i+1;j<N;j++)
{
if(pq.sum[i] == pq.sum2[j])
System.out.println("the sum is" + sum[i] + "and the value of i is" + pq.i + " and j is" + pq.j + "and a is" + pq.a + "and b is " + pq.b
}
        }

        // find smallest sum, print it out, and update
        while (!pq.isEmpty()) {
            CubeSum s = pq.delMin();
            StdOut.println(s);
            
        }
    }

}

am now confused on how to use this program to find all distinct integers a, b, c, and d between 0 and 10^6 such that a3 + b3 = c3 + d3, e.g., 1729 = 9^3 + 10^3 = 1^3 + 12^3.
Posted
Updated 30-Jul-12 2:38am
v5
Comments
Kenneth Haugland 30-Jul-12 5:08am    
Looks like a brute force calculation of all possible combinations...
jasonsmith1987 30-Jul-12 5:41am    
can u plz give me some advice about how i can use this program to find ALL DISTINCT INTEGERS A,B,C AND D BETWEEN 0 AND 10^6 SUCH THAT A^3 + B^3 = c^3 + D^3 EG 1729 = 9^3 + 10^3 = 1^3 + 12^3.
armagedescu 30-Jul-12 6:59am    
You are learning not only the theory, but you are learning programming as well. Don't expect someone else to write your homework.
Richard MacCutchan 30-Jul-12 7:01am    
This same question was posted by one of your classmates last week and he/she received plenty of answers here. Maybe you should get together.
TorstenH. 30-Jul-12 8:53am    
Why are there not more in the variables names?
do you have to pay for each character?

1 solution

@ richard i dont know who posted that previous work.... and this is not a homework assignment....it is just a project iam trying to do.
 
Share this answer
 
Comments
Kenneth Haugland 30-Jul-12 7:56am    
You program looks to just print out every possible combination of a^3+b^3. So after, it is just a matter of finding were the sums are equal, and you have your answer!
jasonsmith1987 30-Jul-12 8:36am    
this is the code i have devised which i think will work,but everytime i run it,i get an error message when i try to iterate through PQ to determine if sum = sum2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900