VBA – How to Find TaxiCab Numbers in Excel – Quick Search in Array

Quick Search for value in Array. In VBA this is sometimes considered problem, as far as people tend to loop through the whole array for the search. There is a better way to do it, with System.Collections.ArrayList, which is not exactly an array, but it can be searched through a built-in function. 🙂 Thus, to show the “quick search for a value in array”, the code below solves a problem for finding TaxiCab numbers.

TaxiCab numbers are numbers expressible as the sum of two [positive] cubes in two different ways. The smallest one is 1729, which is expressible through:

Thus, I was thinking that there should be a fast way to calculate the first few numbers with System.Collections.ArrayList as far as I have written about a helper to sort arrays and range sorter. So, as far as it can be obviously solved with a N^2 complexity algorithm it is quite easy to be done:

As you see, on the code above the System.Collections.ArrayList uses ArrayList.Contains(Element) just as nicely as it would be using it in C# or any other “fancy” language.

Tagged with: , , ,