0

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

Aadith Ramia
  • 9,649
  • 19
  • 63
  • 85

4 Answers4

3

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).

Karl Knechtel
  • 56,349
  • 8
  • 83
  • 124
1

Just do a repeated divmod by 3 for the digits until you end up with 0. Multiply and add to restore the original.

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
1

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

Community
  • 1
  • 1
Dan D.
  • 70,581
  • 13
  • 96
  • 116
0

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
kaleidic
  • 3,010
  • 1
  • 19
  • 22