java - Making Hardy-Ramanujan nth number finder more efficient -


i tried make algorithm find nth hardy-ramanujan number(a number can expressed in more 1 way sum of 2 cubes). except i'm checking every single cube see if equals sum of 2 cubes. tips on how make 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 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 = 1; <= 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 see if there hardy numbers less biggest found     int prevlim = limit;     limit = (int) math.ceil(math.cbrt(lastnum));     for(int = 1; <= 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 nth number pq     long temp = 0;     int count = 0;     while(count<n){         temp = hardynums.poll();         count++;     }     return temp;  } 

these numbers called "taxicab" numbers:

the mathematician g. h. hardy on way visit collaborator srinivasa ramanujan in hospital. hardy remarked ramanujan traveled in taxi cab license plate 1729, seemed dull number. this, ramanujan replied 1729 interesting number — smallest number expressible sum of cubes of 2 numbers in 2 different ways. indeed, 103 + 93 = 123 + 13 = 1729.

since 2 numbers x , y cubes summed must both between 0 , cube root of n, 1 solution exhaustive search of combinations of x , y. better solution starts x = 0 , y cube root of n, repeatedly makes three-way decision: if x3 + y3 < n, increase x, if x3 + y3 > n, decrease y, or if x3 + y3 = n, report success , continue search more:

function taxicab(n)     x, y = 0, cbrt(n)     while x <= y:         s = x*x*x + y*y*y         if s < n x = x + 1         else if n < s y = y - 1         else output x, y              x, y = x + 1, y - 1 

here taxicab numbers less 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 problem @ my blog.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -