Search code examples
maple

Defining automatic sequences on maple


I am using maple to investigate some properties or numerical experiments to see a automatic sequences satisfy certain properties.

First, I would like to define the following sequence on maple. The nth term of the sequence is given by the following expression i_{n}=(-1)^{inv_2(n)}, where inv_2(n) denotes the occurrences of 10 as a scattered subsequence in the binary representation of a number n. For example 2=0x2^{0}+1x2^{1}+0... so the binary representation of 2 is 10, the inversion is therefore 1 and so the above expression I talked about take the value of -1, a more general example will be the binary representation of 12 is 1100, then the inv_2(12) in this case is 4 as we count 10 as a scattered subsequence.

How can I define such a sequence on maple?


Solution

  • If I've interpretted your examples correctly, then this is the procedure inv_2:

    inv_2:= proc(n::nonnegint)
    local N:= convert(n, base, 2), L:= nops(N), i, j, ct:= 0;
         for i from L by -1 to 1 do
              if N[i] = 1 then
                   for j from i-1 by -1 to 1 do
                        if N[j] = 0 then  ct:= ct+1  end if
                   end do
              end if
         end do;
         ct
    end proc:
    

    Please confirm that this gives the correct results.