4

I tried to make an algorithm to find the nth Hardy-Ramanujan number(a number which can be expressed in more than one way as a sum of 2 cubes). Except I'm basically checking every single cube with another to see if it equals a sum of another 2 cubes. Any tips on how to make this more efficient? I'm kind of stumped.

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;

}
Raistlin
  • 167
  • 8

2 Answers2

5

These numbers are sometimes called "taxicab" numbers:

The mathematician G. H. Hardy was on his way to visit his collaborator Srinivasa Ramanujan who was in the hospital. Hardy remarked to Ramanujan that he traveled in a taxi cab with license plate 1729, which seemed a dull number. To this, Ramanujan replied that 1729 was a very interesting number — it was the smallest number expressible as the sum of cubes of two numbers in two different ways. Indeed, 103 + 93 = 123 + 13 = 1729.

Since the two numbers x and y whose cubes are summed must both be between 0 and the cube root of n, one solution is an exhaustive search of all combinations of x and y. A better solution starts with x = 0 and y the cube root of n, then repeatedly makes a three-way decision: if x3 + y3 < n, increase x, if x3 + y3 > n, decrease y, or if x3 + y3 = n, report the success and continue the search for more:

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

Here are the taxicab numbers less than 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))

I discuss this problem at my blog.

user448810
  • 17,381
  • 4
  • 34
  • 59
1

The basic difference between your approach is that you visit (i,j) and also (j,i).
The algorithm suggested by @user448810 visits only once because i will be always less than j in while condition.

Java Implementation of above code:

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

}
René Vogt
  • 43,056
  • 14
  • 77
  • 99