Saturday, February 14, 2015

Ramanujan.java in a pgm



Ramanujan.java

public class Ramanujan
{

   public static void main(String[] args)
   {
      int N = Integer.parseInt(args[0]);
      for (int a = 1; a <= N; a++)
        {
         int a3 = a*a*a;
         if (a3 > N) break;

         for (int b = a; b <= N; b++)
           {
            int b3 = b*b*b;
            if (a3 + b3 > N) break;
            for (int c = a + 1; c <= N; c++)
              {
               int c3 = c*c*c;
               if (c3 > a3 + b3) break;
               for (int d = c; d <= N; d++)
               {
                  int d3 = d*d*d;
                  if (c3 + d3 > a3 + b3) break;

                  if (c3 + d3 == a3 + b3) {
                     System.out.print((a3+b3) + " = ");
                     System.out.print(a + "^3 + " + b + "^3 = ");
                     System.out.print(c + "^3 + " + d + "^3");
                     System.out.println();
                  }
               }
            }
         }
      }
   }
}

No comments:

Post a Comment