Java 使Hardy Ramanujan nth数字查找器更高效
我试图做一个算法来找到第n个Hardy Ramanujan数(一个可以用多种方式表示为2个立方体之和的数)。除了我基本上是检查每个立方体和另一个立方体,看它是否等于另外两个立方体的和。关于如何提高效率有什么建议吗?我有点被难住了Java 使Hardy Ramanujan nth数字查找器更高效,java,performance,math,hardy-ramanujan,Java,Performance,Math,Hardy Ramanujan,我试图做一个算法来找到第n个Hardy Ramanujan数(一个可以用多种方式表示为2个立方体之和的数)。除了我基本上是检查每个立方体和另一个立方体,看它是否等于另外两个立方体的和。关于如何提高效率有什么建议吗?我有点被难住了 public static long nthHardyNumber(int n) { PriorityQueue<Long> sums = new PriorityQueue<Long>(); PriorityQueue<
public static long nthHardyNumber(int n) {
PriorityQueue<Long> sums = new PriorityQueue<Long>();
PriorityQueue<Long> hardyNums = new PriorityQueue<Long>();
int limit = 12;
long lastNum = 0;
//Get the first hardy number
for(int i=1;i<=12;i++){
for(int j = i; j <=12;j++){
long temp = i*i*i + j*j*j;
if(sums.contains(temp)){
if(!hardyNums.contains(temp))
hardyNums.offer(temp);
if(temp > lastNum)
lastNum = temp;
}
else
sums.offer(temp);
}
}
limit++;
//Find n hardy numbers
while(hardyNums.size()<n){
for(int i = 1; i <= limit; i++){
long temp = i*i*i + limit*limit*limit;
if(sums.contains(temp)){
if(!hardyNums.contains(temp))
hardyNums.offer(temp);
if(temp > lastNum)
lastNum = temp;
}
else
sums.offer(temp);
}
limit++;
}
//Check to see if there are hardy numbers less than the biggest you found
int prevLim = limit;
limit = (int) Math.ceil(Math.cbrt(lastNum));
for(int i = 1; i <= prevLim;i++){
for(int j = prevLim; j <= limit; j++){
long temp = i*i*i + j*j*j;
if(sums.contains(temp)){
if(!hardyNums.contains(temp))
hardyNums.offer(temp);
if(temp > lastNum)
lastNum = temp;
}
else
sums.offer(temp);
}
}
//Get the nth number from the pq
long temp = 0;
int count = 0;
while(count<n){
temp = hardyNums.poll();
count++;
}
return temp;
}
public静态长nThardynumber(int n){
PriorityQueue sums=新的PriorityQueue();
PriorityQueue hardyNums=新的PriorityQueue();
整数极限=12;
长lastNum=0;
//得到第一个哈代号码
对于(int i=1;i而言,这些数字有时称为“出租车”编号:
数学家G.H.哈代正在去拜访他的合作者的路上
正在住院的斯里尼瓦萨·拉马努扬。哈代对他说
Ramanujan说他乘坐的出租车车牌是1729,
对于这个问题,拉马努扬回答说,1729年是一个很无聊的数字
非常有趣的数字-这是可以表达为
两个数的立方之和,以两种不同的方式。事实上,
103 + 93 =
123+13=1729
由于立方体求和的两个数字x和y必须都在0和n的立方根之间,因此一个解决方案是对x和y的所有组合进行穷举搜索。更好的解决方案是从x=0和y开始,y是n的立方根,然后反复做出三方决定:如果x3+y3n,减少y,或者如果x3+y3=n,报告成功并继续搜索更多:
function taxicab(n)
x, y = 0, cbrt(n)
while x <= y:
s = x*x*x + y*y*y
if s < n then x = x + 1
else if n < s then y = y - 1
else output x, y
x, y = x + 1, y - 1
我在讨论这个问题。您的方法的基本区别在于您访问(I,j)和(j,I)。
@user448810建议的算法只访问一次,因为在while条件下我将始终小于j
上述代码的Java实现:
import java.util.*;
public class efficientRamanujan{
public static void main(String[] args) {
efficientRamanujan s=new efficientRamanujan();
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int i=0,k=1;
while(i<n){
if(s.efficientRamanujan(k))
{
i=i+1;
System.out.println(i+" th ramanujan number is "+k);
}
k++;
}
scan.close();
}
public boolean efficientRamanujan(int n){
int count=0;
int x = 1;
int y = (int) Math.cbrt(n);
while (x<y){
int sum = (int) Math.pow(x,3) + (int) Math.pow(y,3);
if(sum<n){
x = x+1;
}else if(sum>n){
y = y-1;
}else{
count++;
x = x+1;
y = y-1;
}
if(count>=2){
return true;
}
}
return false;
}
}
import java.util.*;
公共级效率{
公共静态void main(字符串[]args){
efficientRamanujan s=新efficientRamanujan();
扫描仪扫描=新扫描仪(System.in);
int n=scan.nextInt();
int i=0,k=1;
而
import java.util.*;
public class efficientRamanujan{
public static void main(String[] args) {
efficientRamanujan s=new efficientRamanujan();
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int i=0,k=1;
while(i<n){
if(s.efficientRamanujan(k))
{
i=i+1;
System.out.println(i+" th ramanujan number is "+k);
}
k++;
}
scan.close();
}
public boolean efficientRamanujan(int n){
int count=0;
int x = 1;
int y = (int) Math.cbrt(n);
while (x<y){
int sum = (int) Math.pow(x,3) + (int) Math.pow(y,3);
if(sum<n){
x = x+1;
}else if(sum>n){
y = y-1;
}else{
count++;
x = x+1;
y = y-1;
}
if(count>=2){
return true;
}
}
return false;
}
}