Task: Taxicab numbers



C#: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaxicabNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            IDictionary<long, IList<Tuple<int, int>>> taxicabNumbers = GetTaxicabNumbers(2006);
            PrintTaxicabNumbers(taxicabNumbers);
            Console.ReadKey();
        }

        private static IDictionary<long, IList<Tuple<int, int>>> GetTaxicabNumbers(int length)
        {
            SortedList<long, IList<Tuple<int, int>>> sumsOfTwoCubes = new SortedList<long, IList<Tuple<int, int>>>();

            for (int i = 1; i < int.MaxValue; i++)
            {
                for (int j = 1; j < int.MaxValue; j++)
                {
                    long sum = (long)(Math.Pow((double)i, 3) + Math.Pow((double)j, 3));

                    if (!sumsOfTwoCubes.ContainsKey(sum))
                    {
                        sumsOfTwoCubes.Add(sum, new List<Tuple<int, int>>());
                    }

                    sumsOfTwoCubes[sum].Add(new Tuple<int, int>(i, j));

                    if (j >= i)
                    {
                        break;
                    }
                }

                
                if (sumsOfTwoCubes.Count(t => t.Value.Count >= 2) >= length * 1.1)
                {
                    break;
                }
            }

            IDictionary<long, IList<Tuple<int, int>>> values = (from t in sumsOfTwoCubes where t.Value.Count >= 2 select t)
                .Take(2006)
                .ToDictionary(u => u.Key, u => u.Value);

            return values;
        }

        private static void PrintTaxicabNumbers(IDictionary<long, IList<Tuple<int, int>>> values)
        {
            int i = 1;

            foreach (long taxicabNumber in values.Keys)
            {
                StringBuilder output = new StringBuilder().AppendFormat("{0,10}\t{1,4}", i, taxicabNumber);

                foreach (Tuple<int, int> numbers in values[taxicabNumber])
                {
                    output.AppendFormat("\t= {0}^3 + {1}^3", numbers.Item1, numbers.Item2);
                }

                if (i <= 25 || (i >= 2000 && i <= 2006))
                {
                    Console.WriteLine(output.ToString());
                }

                i++;
            }
        }
    }
}








Fortran: 
     PROGRAM POOKA
      IMPLICIT NONE



      INTEGER , PARAMETER  ::  NVARS = 25



      REAL  ::  f1
      REAL  ::  f2
      INTEGER  ::  hits
      INTEGER  ::  s
      INTEGER  ::  TAXICAB

      hits = 0
      s = 0
      f1 = SECOND()
      DO WHILE ( hits<NVARS )
         s = s + 1
         hits = hits + TAXICAB(s)
      END DO
      f2 = SECOND()
      PRINT * , 'elapsed time = ' , f2 - f1 , 'For ' , NVARS , ' Variables'
      STOP
      END PROGRAM POOKA
 
      FUNCTION TAXICAB(N)
      IMPLICIT NONE



      INTEGER  ::  N
      INTEGER  ::  TAXICAB
      INTENT (IN) N



      INTEGER  ::  holder
      INTEGER  ::  oldx
      INTEGER  ::  oldy
      INTEGER  ::  s
      INTEGER  ::  x
      INTEGER  ::  y
      real*8,parameter :: xpon=(1.0D0/3.0D0)

      x = 0
      holder = 0
      oldx = 0
      oldy = 0
      TAXICAB = 0
      y = INT(N**xpon)
      DO WHILE ( x<=y )
         s = x**3 + y**3
         IF( s<N )THEN
            x = x + 1
         ELSE IF( s>N )THEN
            y = y - 1
         ELSE
            IF( holder==s )THEN 
               WRITE(6 , 34)s , '(' , x**3 , y**3 , ')' , '(' , oldx**3 , oldy**3 , ')'
 34            FORMAT(1x , i12 , 10x , 1A1 , i12 , 2x , i12 , 1A1 , 10x , 1A1 , i12 , 2x ,&
                    & i12 , 1A1)
               TAXICAB = 1  
            END IF
            holder = s      
            oldx = x       
            oldy = y
            x = x + 1       
            y = y - 1
         END IF
      END DO
      RETURN
      END FUNCTION TAXICAB