4
$\begingroup$

On the Wolfram Mathematica page on "IntegerDigits" , it says:

"By default, IntegerDigits includes no leading zeros:"

In[1]:= IntegerDigits[Range[0,7],2]

Out[1]= {{0}, {1}, {1, 0}, {1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}}

I want IntegerDigits to include leading zero. For example, I want the following:

In[1]:= NewIntegerDigits[Range[0,8],2]

Out[1]= {{0}, {1}, {0, 0}, {0, 1}, {1, 0}, {1, 1}, {0, 0, 0}, {0, 0, 1}}

Notice that entries "{0, 0}, {0, 1}" and "{0, 0, 0}, {0, 0, 1}" are not generated in the original function. I need the sequence truncated to an arbitrary length (instead of 8), so list[n_]:= NewIntegerDigits[Range[0,n],2] for any n. I also need the function for general base number, e.g. list[n_]:= NewIntegerDigits[Range[0,n],b]

Question: How do I do this? Many thanks for this.

Another example of what I want (base 4):

In[1]:= NewIntegerDigits[Range[1,12],4]

Out[1]= {{0}, {1}, {2}, {3}, {0, 0}, {0, 1}, {0, 2}, {0, 3}, {1, 0}, {1, 1}, {1, 2}, {1, 3}}

$\endgroup$
  • $\begingroup$ To clarify, I want the following list of infinite lists (for a general base, say, base 4) {{0}, {1}, {0, 0}, {0, 1}, {1, 0}, {1, 1}, {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1 , 1} , {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1 , 1} , {0,0,0,0}, {0,0,0,1}, ...} $\endgroup$ – UFP May 3 '17 at 20:40
11
$\begingroup$

how about

Flatten[Table[Tuples[{0, 1}, {k}], {k, 1, 4}], 1]

{{0}, {1}, {0, 0}, {0, 1}, {1, 0}, {1, 1}, {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}}

EDIT

Flatten[Table[Tuples[{0, 1, 2, 3}, {k}], {k, 1, 2}], 1]

{{0}, {1}, {2}, {3}, {0, 0}, {0, 1}, {0, 2}, {0, 3}, {1, 0}, {1, 1}, {1, 2}, {1, 3}, {2, 0}, {2, 1}, {2, 2}, {2, 3}, {3, 0}, {3, 1}, {3, 2}, {3, 3}}

$\endgroup$
  • $\begingroup$ Thank you so much for your help. The generalisation of this is exactly what I wanted, namely Flatten[Table[Tuples[{0, 1, 2, 3}, {k}], {k, 1, 4}], 1] $\endgroup$ – UFP May 3 '17 at 21:17
  • $\begingroup$ nice! you're welcome $\endgroup$ – J42161217 May 3 '17 at 21:36
  • $\begingroup$ Just want to say again that I appreciate you and the other contributers' help a lot. This question was answered within the hour it was posted. Many thanks!!! $\endgroup$ – UFP May 3 '17 at 21:53
  • $\begingroup$ Minor variation: integerDigitZero[base_Integer, maxLength_Integer: 2] := Flatten[Tuples[Range[0, (base - 1)], #] & /@ Range[maxLength], 1]. integerDigitZero[4], for example $\endgroup$ – user1066 May 3 '17 at 23:30
2
$\begingroup$

It is unclear what you're seeking, and what "opposite" means here (since you want {0} and {0,0}). But if you want each list element to have the same number of total digits, then try this:

PadLeft[#, 3] & /@ IntegerDigits[Range[0, 8], 2]

but if you just want to reverse the digits:

Reverse /@ IntegerDigits[Range[0, 8], 2]
$\endgroup$
  • $\begingroup$ Thanks for the quick answer. Please ignore my sentence on "opposite". I want the generalisation, say, with base 2, {{0}, {1}, {0, 0}, {0, 1}, {1, 0}, {1, 1}, {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1 , 1} , {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1 , 1} , {0,0,0,0}, {0,0,0,1}, ...} $\endgroup$ – UFP May 3 '17 at 20:36
  • 8
    $\begingroup$ PadLeft[#, 3] & /@ IntegerDigits[Range[0, 8], 2] can be written more simply as IntegerDigits[Range[0, 8], 2, 3] $\endgroup$ – Bob Hanlon May 3 '17 at 20:38
  • $\begingroup$ You are correct. Thanks... $\endgroup$ – David G. Stork May 3 '17 at 20:39
  • $\begingroup$ Thanks for the answer, but this does not answer the question. I don't want to merely add zeros on the left of each list. In the example I gave, the bolded entries are not generated by IntegerDigits[Range[0, 8], 2, 3] {{0}, {1}, {0, 0}, {0, 1}, {1, 0}, {1, 1}, {0, 0, 0}, {0, 0, 1}} $\endgroup$ – UFP May 3 '17 at 20:48
2
$\begingroup$
Sort@Flatten[Boole@BooleanTable[Array[p, #], Array[p, #]] & /@ Range[7], 1]
$\endgroup$
2
$\begingroup$

Perhaps

list[n_] := Flatten[Table[IntegerDigits[j, 2, i], {i,1,n}, {j,0,i}], 1];
list[4]
$\endgroup$
2
$\begingroup$
This may be what you want.

b = 2; n = 4; IntegerDigits[Range[0, b^n - 1], b]

(*  {{0}, {1}, {1, 0}, {1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}, {1, 0, 
  0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 
  1}, {1, 1, 1, 0}, {1, 1, 1, 1}}  *)

b = 4; n = 3; IntegerDigits[Range[0, b^n - 1], b]

(*  {{0}, {1}, {2}, {3}, {1, 0}, {1, 1}, {1, 2}, {1, 3}, {2, 0}, {2, 1}, {2, 
  2}, {2, 3}, {3, 0}, {3, 1}, {3, 2}, {3, 3}, {1, 0, 0}, {1, 0, 1}, {1, 0, 
  2}, {1, 0, 3}, {1, 1, 0}, {1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 2, 0}, {1, 2,
   1}, {1, 2, 2}, {1, 2, 3}, {1, 3, 0}, {1, 3, 1}, {1, 3, 2}, {1, 3, 3}, {2, 
  0, 0}, {2, 0, 1}, {2, 0, 2}, {2, 0, 3}, {2, 1, 0}, {2, 1, 1}, {2, 1, 2}, {2,
   1, 3}, {2, 2, 0}, {2, 2, 1}, {2, 2, 2}, {2, 2, 3}, {2, 3, 0}, {2, 3, 
  1}, {2, 3, 2}, {2, 3, 3}, {3, 0, 0}, {3, 0, 1}, {3, 0, 2}, {3, 0, 3}, {3, 1,
   0}, {3, 1, 1}, {3, 1, 2}, {3, 1, 3}, {3, 2, 0}, {3, 2, 1}, {3, 2, 2}, {3, 
  2, 3}, {3, 3, 0}, {3, 3, 1}, {3, 3, 2}, {3, 3, 3}}  *)
$\endgroup$
  • $\begingroup$ Thanks for the attempt. This is not what I want atm, since "the entries "{0, 0}, {0, 1}" and "{0, 0, 0}, {0, 0, 1}" are not generated" in the proposed function. $\endgroup$ – UFP May 4 '17 at 16:01
0
$\begingroup$

In:

appendZeroOne[x_] := Map[Append[x, #] &, {0, 1}]
reproduce[xs_] := Map[appendZeroOne, xs] // Catenate
NestIntegerDigits[n_] := 
 NestList[reproduce, {{0}, {1}}, n - 1] // Catenate
NestIntegerDigits[4]

Out:

{{0}, {1}, 
{0, 0}, {0, 1}, {1, 0}, {1, 1},
{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}, 
{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 0, 0}, {0, 
  1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 
  1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 
  1, 0}, {1, 1, 1, 1}}
$\endgroup$

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.