3
$\begingroup$

For example, to print all ternary number of 3 digits I would do something like this

Do[Do[Do[Print[i, j, k], {k, 0, 2}], {j, 0, 2}], {i, 0, 2}]

to return 000, 001, 002,010,011,...

But I how to do it if I want the ternary numbers of 100 digits? It would be to hard to do it in the same way I did in the 3 digits case.

$\endgroup$
  • 5
    $\begingroup$ You want all ternary numbers with 100 digits? Isn't that 3^100 different numbers?! $\endgroup$ – march Oct 30 '15 at 17:19
  • 1
    $\begingroup$ list = Tuples[{0,1,2}, n] does what you want, where n is the number of digits. But don't do this with n=100. $\endgroup$ – march Oct 30 '15 at 17:21
  • 2
    $\begingroup$ @N.J.Evans. You're going to crash the universe with n=100! $\endgroup$ – march Oct 30 '15 at 17:22
  • 2
    $\begingroup$ @march 100! ? challange accepted: Tuples[{0,1,2}, 100!] $\endgroup$ – paw Oct 30 '15 at 17:24
  • 2
    $\begingroup$ @march It may be a manageable number, but I assume OP is going somewhere with this, and I'm kind of curious if this is the best means to his final end. $\endgroup$ – N.J.Evans Oct 30 '15 at 17:25
4
$\begingroup$
n = 3;
list = Tuples[{0, 1, 2}, n];

To get numbers rather than lists of digits:

list2 = FromDigits /@ list

(*  {0, 1, 2, 10, 11, 12, 20, 21, 22, 100, 101, 102, 110, 111, 112, 120, 121, \
122, 200, 201, 202, 210, 211, 212, 220, 221, 222}  *)

Or

BaseForm[#, 3] & /@ Range[0, 3^n - 1]

enter image description here

To pad with leading 0's

IntegerString[#, 3, n] & /@ Range[0, 3^n - 1]

(*  {"000", "001", "002", "010", "011", "012", "020", "021", "022", \
"100", "101", "102", "110", "111", "112", "120", "121", "122", "200", \
"201", "202", "210", "211", "212", "220", "221", "222"}  *)

Or

IntegerString[#, 10, n] & /@ list2

(*  {"000", "001", "002", "010", "011", "012", "020", "021", "022", \
"100", "101", "102", "110", "111", "112", "120", "121", "122", "200", \
"201", "202", "210", "211", "212", "220", "221", "222"}  *)
|improve this answer|||||
$\endgroup$
5
$\begingroup$

Here is another way to generate ternary number strings of a certain length.

ternaryStrings[len_Integer?Positive] := 
  StringJoin @@@ Map[ToString, Tuples[{0, 1, 2}, len], {2}]

With this

ternaryStrings[1]

{"0", "1", "2"}

ternaryStrings[2]

{"00", "01", "02", "10", "11", "12", "20", "21", "22"}

ternaryStrings[3]

{"000", "001", "002", "010", "011", "012", "020", "021", "022", "100", "101", "102", "110", "111", "112", "120", "121", "122", "200", "201", "202", "210", "211", "212", "220", "221", "222"}

And so on.

You, of course, want to list all the ternary number strings of length 100, which I don't think is possible because there are $3^{100}$ such strings. Certainly, ternaryStrings[100] won't do it. Mathematica won't even try to generate all the 3-tuples of length 100.

Tuples[{0, 1, 2}, 100]

Tuples::toomany: The length of the output of Tuples[{0,1,2},100] should be a machine integer. >>

Tuples[{0, 1, 2}, 100]

Update

As J.M. points this my code can be simplified to

ternaryStrings[len_Integer?Positive] := StringJoin @@@ Tuples[{"0", "1", "2"}, len]

which will be considerably faster as well.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Why not just Tuples[{"0", "1", "2"}, len]? $\endgroup$ – J. M.'s technical difficulties Oct 30 '15 at 21:56
  • $\begingroup$ @J.M. Indeed, why not? Because that simplication didn't occur to me. $\endgroup$ – m_goldberg Oct 30 '15 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.