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
4 Answers
Numbers don't intrinsically have a base. Representations of numbers do. Typically, you express the concept of "base 3 representation of a number" by using a string consisting of only the '0', '1' and '2' characters (and possibly a decimal point or negative sign or whatever).

- 62,466
- 11
- 102
- 153
Just do a repeated divmod by 3 for the digits until you end up with 0. Multiply and add to restore the original.

- 776,304
- 153
- 1,341
- 1,358
python can convert from base 3 using int('012',3)
which is 5 and Python elegant inverse function of int(string,base) has the reverse of that
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

- 3,070
- 1
- 21
- 22