让Hardy-Ramanujan第n个数查找器更高效

4

我尝试制作一个算法来查找第n个哈代-拉马努金数(可以用多种方式表示为两个立方和的数字)。除了基本上检查每个立方体是否与另一个立方体相等以形成另外两个立方体的和。有什么方法可以使这个算法更有效率吗?我有点困惑。

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;

}
2个回答

5
这些数字有时被称为“出租车”数:
数学家G.H.哈代前往探望他的合作者Srinivasa Ramanujan,而后者正在医院里。哈代对Ramanujan说他坐在一个出租车上,车牌号是1729,这似乎是一个无聊的数字。然而,Ramanujan回答说,1729是一个非常有趣的数字——它是最小的可以用两种不同的方式将两个数字的立方和表示出来的数字。事实上,10³+9³=12³+1³=1729。
由于两个立方和的数字x和y必须都在0和n的立方根之间,因此一种解决方案是全部遍历x和y的所有组合。更好的解决方案是从x=0开始,y=n的立方根,然后重复进行三项决策:如果x³+y³n,则减小y;如果x³+y³=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

以下是小于100000的出租车数字:

1729: ((1 12) (9 10))
4104: ((2 16) (9 15))
13832: ((2 24) (18 20))
20683: ((10 27) (19 24))
32832: ((4 32) (18 30))
39312: ((2 34) (15 33))
40033: ((9 34) (16 33))
46683: ((3 36) (27 30))
64232: ((17 39) (26 36))
65728: ((12 40) (31 33))

我在我的博客上讨论了这个问题。

1
你的方法和@user448810提出的算法之间的基本区别在于,你访问了(i,j)和(j,i)两个位置。
@user448810建议的算法只访问一次,因为在while条件中i始终小于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;
}

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接