Problems

  1. Computational number theory. Write a program CubeSum.java that prints out all integers of the form a3+b3a^3+b^3where aa andbb𝑏 are integers between 0 and n<n< in sorted order, without using excessive space. That is, instead of computing an array of the n2n^2 sums and sorting them, build a minimum-oriented priority queue, initially containing (03,0,0),(13+13,1,1),(23+23,2,2),…,(n3+n3,n,n)(0^3,0,0),(1^3+1^3,1,1),(2^3+2^3,2,2),…,(n^3+n^3,n,n). Then, while the priority queue is nonempty, remove the smallest item i3+j3,i,j)i^3+j^3,i,j), print it, and then, if j<nj<n, insert the item (i3+(j+1)3,i,j+1)(i^3+(j+1)^3,i,j+1). Use this program to find all distinct integers a,b,ca,b,c, and dd between 0 and 10610^6such that a3+b3=c3+d3a^3+b^3=c^3+d^3π‘Ž3+𝑏3=, such as 1729=93+103=13+1231729=9^3+10^3=1^3+12^3.

#include <iostream>
using namespace std;

class cubeSum {

public:
     int i, j, sum;
     cubeSum(int i = 0, int j = 0) {
          this->sum = i * i * i + j * j * j;
          this->i = i;
          this->j = j;
     }
     int compareTo(const cubeSum &that) {
          if (this->sum > that.sum) return 1;
          else if (this->sum < that.sum) return -1;
          return 0;
     }
     string toString() {
          string res = to_string(sum) + " = " + to_string(i) + "^3" " + " + to_string(j) + "^3";
          return res;
     }
};

template <typename key>
class MinPQ {
     key *arr;
     int N;
     int i = 0;

     bool greeter(key &key1, key &key2) {
          return key1.compareTo(key2) > 0;
     }

     void swim(int k) {
          while (k > 1 && greeter(arr[k / 2], arr[k])) {
               swap(arr[k], arr[k / 2]);
               k = k / 2;
          }
     }

     void sink(int k) {
          while (2 * k <= i) {
               int j = 2 * k;

               if (j < i && greeter(arr[j], arr[j + 1])) j++; // find out which one is smaller 2k or 2k+1

               if (!greeter(arr[k], arr[j])) break; // break if the parent's key is smaller than both children's key

               swap(arr[k], arr[j]);
               k = j; // moving down the heap
          }
     }

public:
     MinPQ(int capacity) : N(capacity + 1) {
          arr = new key[capacity + 1]{NULL};
     }

     bool isEmpty() {
          return i == 0;
     }
     bool isFull() {
          return i == N - 1;
     }

     // At most 1 + lg N compares
     void insert(key val) {
          arr[++i] = val;
          swim(i);
     }

     // At most 2 lg N compares
     key delMin() {
          key min = arr[1];
          swap(arr[1], arr[i--]);
          sink(1);
          arr[i + 1] = NULL; // prevent loitering
          return min;
     }
};

int main() {
     int n = 4;
     MinPQ<cubeSum> *PQ = new MinPQ<cubeSum>(n);
     for (int i = 0; i < n; i++) {
          cubeSum cb(i, i);
          PQ->insert(cb);
     }

     while (!PQ->isEmpty()) {
          cubeSum s = PQ->delMin();
          if (s.j < n) {
               cubeSum cb(s.i, s.j + 1);
               PQ->insert(cb);
          }
          cout << s.toString() << endl;
     }
}
  1. Index priority-queue implementation. Implement IndexMaxPQ.java by modifying MaxPQ.java as follows: Change pq[] to hold indices, add an array keys [ ] to hold the key values, and add an array qp[] that is the inverse of pq [] β€” qp[i] gives the position of i in pq[] (the index j such that pq[j] is i). Then modify the code to maintain these data structures. Use the convention that qp[i] is -1 if i is not on the queue, and include a method contains() that tests this condition. You need to modify the helper methods exch() and less() but not sink() or swim().

Last updated

Was this helpful?