Skip to content

Instantly share code, notes, and snippets.

@wgpshashank
Last active February 6, 2019 05:32
Show Gist options
  • Save wgpshashank/1292111 to your computer and use it in GitHub Desktop.
Save wgpshashank/1292111 to your computer and use it in GitHub Desktop.
Find the smallest number expressible as the sum of cubes of two numbers in two different ways. Indeed, 10^3 + 9^3 =12^3+1^3=1729 , Do It Efficiently
Problem:
A mathematician G. H. Hardy was on his way to visit his collaborator S. Ramanujan an who was in the hospital. Hardy remarked to Ramanujan that he traveled in taxi cab number 1729 which seemed a dull one and he hoped it was not a bad omen. To this, Ramanujan( Math Guy) 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, 10^3 + 9^3 =12^3 + 1^3 = 1729.
Hint: This problem is very similar to another very popular problem that is asked in interviews.
You are given a η ×η matrix in which both rows and columns are sorted in ascending order and you are supposed
to find a given number in the matrix.
Algorithm:
In this case, we are essentially looking for an implicit matrix A such that A(i,j) =i^3十j^3. In our case, the matrix will have n^1/3 rows and columns and this matrix of size n^1/3 * n^1/3 we try to search k=i^3+j^3 isn't it? Algorithms for searching for a number in such a matrix that are linear in the number of rows.
One approach is to start by comparing x to An,1. If x = An ,l , stop.Otherwise, there are two cases:
- x < A1,n in which case x is less than element at Column n at 1,n position. we decrement column pointer to left.e.g. previous column.
- x > A1,n , in which case if x > a[1][n] is greater than it will be greater then all elements in Row 1.. we increment the row pointer to next row.
Efficient Solution:
Here is the Pseudo Code For The Same.
int IsNumberEqualstoSumeTwoCubes(int n)
{
int row=Ceil(Pow(n,1/3));// Pow Has Complexity of O(logn) Ceil(1729^(1/3))=12
int column=row;
int i=0;int j=column;
while(i<row && j>=0)
{
int m=i*i*i+j*j*j;
if(m==n)
return 1; // print i^3 and j^3 these are two such numbers
else if ( m < n)
i++;
else
j--;
}
return 0; //such number DNE
}
Complexity Analysis:
In either case, we have a matrix with η fewer elements to search. In each iteration, we remove a row or a column, which means we inspect 2η-1 elements.
Above Algorithm Really Requires Deep Understanding of Young_tableau :)
Time Complexity O(row+column)=O(N^1/3+N^1/3)=O(2*N^1/3)=O(N^1/3)
Space Complexity O(1)
Source:Algorithms by Anand & Aziz
@shwgupta
Copy link

shwgupta commented Feb 6, 2019

So, as per the code any number which is a perfect cube for e.g 729, 1000 is the answer, but it is not the case.
Can you please explain, if I am incorrect. (0^3 + 10^3 = 1000, However, the ask is to check if 1000 can be represented as cubes for 2 different sets as 1729).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment