Search code examples
mathprogramming-languagesnumbers

easiest way to generate base 3 numbers


I am trying to solve a mathematical problem programatically. Having a way to generate base 3 numbers would make it very easy to solve the problem. Do you know of any language that has in-built support for working with numbers of non-trivial base? It would be equally great if you could point me to some tool that can help me generate a sequence of base 3 numbers


Solution

  • The J programming language (http://jsoftware.com/) has both Base and Antibase as primary functions. These are entirely numeric, not character manipulations. The Vocabulary entries for these primaries are http://jsoftware.com/help/dictionary/d401.htm and http://jsoftware.com/help/dictionary/d402.htm

    Using these it is simple to work with base-3 representations. Here's an example program definition, and interactive use in a console:

       base3=: 3& #. ^:_1
    
       base3 8
    2 2
       base3 i. 19
    0 0 0
    0 0 1
    0 0 2
    0 1 0
    0 1 1
    0 1 2
    0 2 0
    0 2 1
    0 2 2
    1 0 0
    1 0 1
    1 0 2
    1 1 0
    1 1 1
    1 1 2
    1 2 0
    1 2 1
    1 2 2
    2 0 0