计算数论 [英] computational number theory
问题描述
此程序CubeSum.java可按排序顺序打印出a3 + b3形式的所有整数,其中a和b是按排序顺序在0到N之间的整数.
也就是说,与其计算N2和的数组并对其进行排序,不如建立一个面向最低优先级的队列,该队列最初包含(03,0,0),(13,1,0),(23,2,0), ...,(N3,N,0).
然后,当优先级队列为非空时,删除最小的项(i3 + j3,i,j),打印它,然后如果j< N,插入项目(i3 +(j + 1)3,i,j + 1).
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).
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);
}
}
}
现在对如何使用该程序查找0和10 ^ 6之间的所有不同整数a,b,c和d感到困惑,例如a3 + b3 = c3 + d3,例如1729 = 9 ^ 3 + 10 ^ 3 = 1 ^ 3 + 12 ^ 3.
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.
推荐答案
@理查德我不知道是谁张贴了以前的作品....这不是家庭作业....这只是我想做的一个项目.
@ 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.
这篇关于计算数论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!