New answers tagged

6

You are asking for the digits in the base 2 representation of the number 3, with some number, call it $n_0$, of leading zeros. Clear[f] f[n0_] := IntegerDigits[3, 2, n0 + 2] f[0] (* {1, 1} *) f[1] (* {0, 1, 1} *) f[2] (* {0, 0, 1, 1} *) f[10] (* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1} *)


7

A few additional alternatives: ClearAll[ap, ur, ar, cr, ab, sa] ap = ArrayPad[{1, 1}, {#, 0}] &; ur = UnitStep[Range[# + 2] - # - 1/2] &; ar = Reverse @ ArrayReshape[{1, 1}, # + 2] &; cr = Clip[Range[# + 2], {#, #} + 1/2, {0, 1}] &; ab = Array[a \[Function] Boole[a > #], # + 2] &; sa = Normal @ SparseArray[{# + 1 | # + 2 -> 1},...


3

Using Join[] and Table[] seems to be the easiest way to do it. fn[n_] := Join[Table[0, n],{1, 1}]; fn[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}


Top 50 recent answers are included