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++;
            }
        }
    }
}






VB: Public Type tuple
    i As Variant
    j As Variant
    sum As Variant
End Type
Public Type tuple3
    i1 As Variant
    j1 As Variant
    i2 As Variant
    j2 As Variant
    i3 As Variant
    j3 As Variant
    sum As Variant
End Type
Sub taxicab_numbers()
    Dim i As Variant, j As Variant
    Dim k As Long
    Const MAX = 2019
    Dim p(MAX) As Variant
    Const bigMAX = (MAX + 1) * (MAX / 2)
    Dim big(1 To bigMAX) As tuple
    Const resMAX = 4400
    Dim res(1 To resMAX) As tuple3
    For i = 1 To MAX
        p(i) = CDec(i * i * i) 
    Next i                     
    
    k = 1
    For i = 1 To MAX
        For j = i To MAX
            big(k).i = CDec(i)
            big(k).j = CDec(j)
            big(k).sum = CDec(p(i) + p(j))
            k = k + 1
        Next j
    Next i
    n = 1
    Quicksort big, LBound(big), UBound(big)
    For i = 1 To bigMAX - 1
        If big(i).sum = big(i + 1).sum Then
            res(n).i1 = CStr(big(i).i)
            res(n).j1 = CStr(big(i).j)
            res(n).i2 = CStr(big(i + 1).i)
            res(n).j2 = CStr(big(i + 1).j)
            If big(i + 1).sum = big(i + 2).sum Then
                res(n).i3 = CStr(big(i + 2).i)
                res(n).j3 = CStr(big(i + 2).j)
                i = i + 1
            End If
            res(n).sum = CStr(big(i).sum)
            n = n + 1
            i = i + 1
        End If
    Next i
    Debug.Print n - 1; " taxis"
    For i = 1 To 25
        With res(i)
            Debug.Print String$(4 - Len(CStr(i)), " "); i;
            Debug.Print String$(11 - Len(.sum), " "); .sum; " = ";
            Debug.Print String$(4 - Len(.i1), " "); .i1; "^3 +";
            Debug.Print String$(4 - Len(.j1), " "); .j1; "^3 = ";
            Debug.Print String$(4 - Len(.i2), " "); .i2; "^3 +";
            Debug.Print String$(4 - Len(.j2), " "); .j2; "^3"
        End With
    Next i
    Debug.Print
    For i = 2000 To 2006
        With res(i)
            Debug.Print String$(4 - Len(CStr(i)), " "); i;
            Debug.Print String$(11 - Len(.sum), " "); .sum; " = ";
            Debug.Print String$(4 - Len(.i1), " "); .i1; "^3 +";
            Debug.Print String$(4 - Len(.j1), " "); .j1; "^3 = ";
            Debug.Print String$(4 - Len(.i2), " "); .i2; "^3 +";
            Debug.Print String$(4 - Len(.j2), " "); .j2; "^3"
        End With

    Next i
    Debug.Print
    For i = 1 To resMAX
        If res(i).i3 <> "" Then
            With res(i)
                Debug.Print String$(4 - Len(CStr(i)), " "); i;
                Debug.Print String$(11 - Len(.sum), " "); .sum; " = ";
                Debug.Print String$(4 - Len(.i1), " "); .i1; "^3 +";
                Debug.Print String$(4 - Len(.j1), " "); .j1; "^3 = ";
                Debug.Print String$(4 - Len(.i2), " "); .i2; "^3 +";
                Debug.Print String$(4 - Len(.j2), " "); .j2; "^3";
                Debug.Print String$(4 - Len(.i3), " "); .i3; "^3 +";
                Debug.Print String$(4 - Len(.j3), " "); .j3; "^3"
            End With
        End If
    Next i
End Sub
Sub Quicksort(vArray() As tuple, arrLbound As Long, arrUbound As Long)
    
    
    
    
    Dim pivotVal As Variant
    Dim vSwap    As tuple
    Dim tmpLow   As Long
    Dim tmpHi    As Long
     
    tmpLow = arrLbound
    tmpHi = arrUbound
    pivotVal = vArray((arrLbound + arrUbound) \ 2).sum
     
    While (tmpLow <= tmpHi) 
        While (vArray(tmpLow).sum < pivotVal And tmpLow < arrUbound)
            tmpLow = tmpLow + 1
        Wend
        
        While (pivotVal < vArray(tmpHi).sum And tmpHi > arrLbound)
            tmpHi = tmpHi - 1
        Wend
     
        If (tmpLow <= tmpHi) Then
             vSwap.i = vArray(tmpLow).i
             vSwap.j = vArray(tmpLow).j
             vSwap.sum = vArray(tmpLow).sum
             vArray(tmpLow).i = vArray(tmpHi).i
             vArray(tmpLow).j = vArray(tmpHi).j
             vArray(tmpLow).sum = vArray(tmpHi).sum
             vArray(tmpHi).i = vSwap.i
             vArray(tmpHi).j = vSwap.j
             vArray(tmpHi).sum = vSwap.sum
             tmpLow = tmpLow + 1
             tmpHi = tmpHi - 1
        End If
    Wend
 
    If (arrLbound < tmpHi) Then Quicksort vArray, arrLbound, tmpHi 
    If (tmpLow < arrUbound) Then Quicksort vArray, tmpLow, arrUbound 
End Sub