tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Core > Special Numbers > Ramanujan Number

Ramanujan Number 

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

File Name  :  
com/bethecoder/tutorials/core/special_numbers/RamanujanNumber.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.core.special_numbers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class RamanujanNumber {

  /**
   @param args
   */
  public static void main(String[] args) {

    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 = ; a < cubeNumList.size() ; a ++) {
      
      for (int b = ; b < cubeNumList.size() ; b ++) {

        if (cubeNumList.get(a).getNumCube() +  cubeNumList.get(b).getNumCube() > range) {
          break;
        }
        
        for (int c = ; c < cubeNumList.size() ; c ++) {
          
          if (cubeNumList.get(a).getNumCube() +  cubeNumList.get(b).getNumCube() 
              < cubeNumList.get(c).getNumCube()) {
            break;
          }

          for (int d = ; 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 = ; 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 = ; 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 = (RamanujanCombinationother; 
      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();
  }
}
   

It gives the following output,
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
13832 = 2^3 + 24^3 = 18^3 + 20^3
20683 = 10^3 + 27^3 = 19^3 + 24^3
32832 = 4^3 + 32^3 = 18^3 + 30^3
39312 = 2^3 + 34^3 = 15^3 + 33^3
40033 = 9^3 + 34^3 = 16^3 + 33^3
46683 = 3^3 + 36^3 = 27^3 + 30^3
64232 = 17^3 + 39^3 = 26^3 + 36^3
65728 = 12^3 + 40^3 = 31^3 + 33^3
110656 = 4^3 + 48^3 = 36^3 + 40^3
110808 = 6^3 + 48^3 = 27^3 + 45^3
134379 = 12^3 + 51^3 = 38^3 + 43^3
149389 = 8^3 + 53^3 = 29^3 + 50^3
165464 = 20^3 + 54^3 = 38^3 + 48^3
171288 = 17^3 + 55^3 = 24^3 + 54^3
195841 = 9^3 + 58^3 = 22^3 + 57^3
216027 = 3^3 + 60^3 = 22^3 + 59^3
216125 = 5^3 + 60^3 = 45^3 + 50^3
262656 = 8^3 + 64^3 = 36^3 + 60^3
314496 = 4^3 + 68^3 = 30^3 + 66^3
320264 = 18^3 + 68^3 = 32^3 + 66^3
327763 = 30^3 + 67^3 = 51^3 + 58^3
373464 = 6^3 + 72^3 = 54^3 + 60^3
402597 = 42^3 + 69^3 = 56^3 + 61^3
439101 = 5^3 + 76^3 = 48^3 + 69^3
443889 = 17^3 + 76^3 = 38^3 + 73^3
513000 = 10^3 + 80^3 = 45^3 + 75^3
513856 = 34^3 + 78^3 = 52^3 + 72^3
515375 = 15^3 + 80^3 = 54^3 + 71^3
525824 = 24^3 + 80^3 = 62^3 + 66^3
558441 = 30^3 + 81^3 = 57^3 + 72^3
593047 = 7^3 + 84^3 = 63^3 + 70^3
684019 = 51^3 + 82^3 = 64^3 + 75^3
704977 = 2^3 + 89^3 = 41^3 + 86^3
805688 = 11^3 + 93^3 = 30^3 + 92^3
842751 = 23^3 + 94^3 = 63^3 + 84^3
885248 = 8^3 + 96^3 = 72^3 + 80^3
886464 = 12^3 + 96^3 = 54^3 + 90^3
920673 = 20^3 + 97^3 = 33^3 + 96^3
955016 = 24^3 + 98^3 = 63^3 + 89^3
984067 = 35^3 + 98^3 = 59^3 + 92^3
994688 = 29^3 + 99^3 = 60^3 + 92^3
1009736 = 50^3 + 96^3 = 59^3 + 93^3
1016496 = 47^3 + 97^3 = 66^3 + 90^3
1061424 = 6^3 + 102^3 = 45^3 + 99^3
1073375 = 23^3 + 102^3 = 60^3 + 95^3
1075032 = 24^3 + 102^3 = 76^3 + 86^3
1080891 = 27^3 + 102^3 = 48^3 + 99^3
1092728 = 1^3 + 103^3 = 64^3 + 94^3
1195112 = 16^3 + 106^3 = 58^3 + 100^3
1260441 = 9^3 + 108^3 = 81^3 + 90^3
1323712 = 40^3 + 108^3 = 76^3 + 96^3
1331064 = 4^3 + 110^3 = 67^3 + 101^3
1370304 = 34^3 + 110^3 = 48^3 + 108^3
1407672 = 14^3 + 112^3 = 63^3 + 105^3
1533357 = 62^3 + 109^3 = 90^3 + 93^3
1566728 = 18^3 + 116^3 = 44^3 + 114^3
1609272 = 55^3 + 113^3 = 92^3 + 94^3
1728216 = 6^3 + 120^3 = 44^3 + 118^3
1729000 = 10^3 + 120^3 = 90^3 + 100^3



 
  


  
bl  br