Ramanujan number is a number which can be expressed as sum of cubes of two numbers in
different combinations.
a^3 + b^3 = Ramanujan Number = c^3 + d ^3
long range = 1729245; // 100000; //48988659276962496L;
List<CubeNum> cubeNumList = getCubes(range);
//System.out.println(cubeNumList);
List <RamanujanCombination> combinationList = new ArrayList <RamanujanCombination>();
RamanujanCombination combination = null;
for (int a = 0 ; a < cubeNumList.size() ; a ++) {
for (int b = 0 ; b < cubeNumList.size() ; b ++) {
if (cubeNumList.get(a).getNumCube() + cubeNumList.get(b).getNumCube() > range) { break;
}
for (int c = 0 ; c < cubeNumList.size() ; c ++) {
if (cubeNumList.get(a).getNumCube() + cubeNumList.get(b).getNumCube()
< cubeNumList.get(c).getNumCube()) { break;
}
for (int d = 0 ; d < cubeNumList.size() ; d ++) {
//No two digits should be equal if (a == b || a == c || a == d || b == c || b == d || a == d) { continue;
}
if (cubeNumList.get(a).getNumCube() + cubeNumList.get(b).getNumCube() ==
cubeNumList.get(c).getNumCube() + cubeNumList.get(d).getNumCube()) {
combination = new RamanujanCombination(
cubeNumList.get(a).getNum(),
cubeNumList.get(b).getNum(),
cubeNumList.get(c).getNum(),
cubeNumList.get(d).getNum()); if (!combinationList.contains(combination)) {
combinationList.add(combination);
//System.out.println(combination);
}
}
}// for - d
}// for - c
}// for - b
}// for - a
//OPTIONAL
//The above ramanujan list may not be in sorted order
combinationList = sort(combinationList);
for (int i = 0 ; i < combinationList.size() ; i ++ ) {
System.out.println(combinationList.get(i));
}
}
private static List <RamanujanCombination> sort(List <RamanujanCombination> combinationList) {
RamanujanCombination [] combinations = new RamanujanCombination [combinationList.size()];
combinationList.toArray(combinations);
Arrays.sort(combinations, new Comparator<RamanujanCombination>() {
@Override public int compare(RamanujanCombination r1,
RamanujanCombination r2) {
if (r1.getRamanujanNum() > r2.getRamanujanNum()) { return 1;
} else if (r1.getRamanujanNum() < r2.getRamanujanNum()) { return -1;
} return 0;
}
});
return Arrays.asList(combinations);
}
private static List<CubeNum> getCubes(long number) {
List<CubeNum> cubeNumList = new ArrayList<CubeNum>(); long cubeOfNum = 0;
for (long i = 1 ; i <= number ; i ++ ) {
cubeOfNum = cube(i); if (cubeOfNum >= number) { break;
}
cubeNumList.add(new CubeNum(i, cubeOfNum));
}
return cubeNumList;
}
public static long cube(long num) { return num * num * num;
}
}
class CubeNum {
long num; long numCube;
public CubeNum(long num, long numCube) { super(); this.num = num; this.numCube = numCube;
}
public long getNum() { return num;
}
public void setNum(int num) { this.num = num;
}
public long getNumCube() { return numCube;
}
public void setNumCube(int numCube) { this.numCube = numCube;
}
public String toString() { return "[" + num + "=" + numCube + "]";
}
}
class RamanujanCombination {
private long a; private long b; private long c; private long d; private long ramanujanNum;
public long getRamanujanNum() { return ramanujanNum;
}
public void setRamanujanNum(int ramanujanNum) { this.ramanujanNum = ramanujanNum;
}
public RamanujanCombination(long a, long b, long c, long d) { super(); this.a = a; this.b = b; this.c = c; this.d = d; this.ramanujanNum = RamanujanNumber.cube(a) + RamanujanNumber.cube(b);
}
public boolean equals(Object other) {
if (other instanceof RamanujanCombination) {
RamanujanCombination o = (RamanujanCombination) other; return Arrays.asList(new Long [] { a, b, c, d }).containsAll(
Arrays.asList(new Long [] { o.a, o.b, o.c, o.d }));
}
return false;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(ramanujanNum);
sb.append(" = ");
sb.append(a + "^3 + " + b + "^3");
sb.append(" = ");
sb.append(c + "^3 + " + d + "^3"); return sb.toString();
}
}