Algs4-2.4.25 computational number theory

 2.4.25 computational number theory. Programming CubeSum.java, without the use of extra space, a ^ + b ^ print results of all 33 of the order of size, where a and b are integers between 0 and N

. That is, not all of the N ^ 2 th is calculated and then sort, but to create a minimum priority queue, the initial state (0 ^ 3,0,0), (3,1,0 ^ 1), (2 ^ 3 , 2,0), ..., (N ^ 3, N, 0).

So long as the priority queue is not empty, delete and print the smallest element (i ^ 3 + j ^ 3 , i, j). Then, if j <N, the insertion elements (i ^ 3 + (j + 1) ^ 3, i, j + 1). With this program to find the 0-10 ^ 6

in all satisfy a ^ 3 + b ^ 3 = c ^ 3 + d ^ 3 different integers a, b, c, d.

A: The
first programming parts:
1) a class i, j, i ^ 3 + j ^ 3 FIG.
2) storing a class above the minimum priority queue.
3) a cycle beginning classes used to implement the above examples of N, while the enqueue instance.
4) used to dequeue a loop until the team is empty. After dequeue then a new instance of enqueue and new instances generated by the subject of the request.

The second find 0-10 ^ 6 cubic sum equal:
using the above procedure N is set to 10 ^ 6, and then to rank, and to check on the current instance of a dequeue dequeue at each instance,
when <> and the two instances of the same instance and the instance. J .i <> this instance .i <> this instance .j, then two instances of i, j to satisfy the requirements of the subject.


The difficulty of the question should be why the analysis shows a process problem by such algorithms do meet the requirements.
"Computational number theory" is another branch of algorithms and number theory.

 


Here is the code book official network, it uses the real time initialization and the diagonal of the matrix.


官网代码:
/******************************************************************************
 *  Compilation:  javac CubeSum.java
 *  Execution:    java CubeSum n
 *  Dependencies: MinPQ.java
 *
 *  Print out integers of the form a^3 + b^3 in sorted order, where
 *  0 <= a <= b <= n.
 *
 *  % java CubeSum 10
 *  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
 *  ...
 *  1729 = 9^3 + 10^3
 *  1729 = 1^3 + 12^3
 *  ...
 *  3456 = 12^3 + 12^3
 *
 *  Remarks
 *  -------
 *   - Easily extends to handle sums of the form f(a) + g(b)
 *   - Prints out a sum more than once if it can be obtained
 *     in more than one way, e.g., 1729 = 9^3 + 10^3 = 1^3 + 12^3
 *
 ******************************************************************************/

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));
        }
    }

}

Guess you like

Origin www.cnblogs.com/longjin2018/p/11439982.html