Denote by S={2,0,1,5} and
A(n,i)={xnxn−1…x1:xj∈S and x1+⋯+xn≡i(mod3)}.
Let an,bn and cn be the cardinal number of A(n,0), A(n,1) and A(n,2) respectively. Since a natural number is divisible by 3 if and only if the sum of its digits is a multiple of 3, we only need to count the number of elements in the set A(k,0). Let x1…xn+1 be an element of A(n+1,0), we have
* If xn+1=0 then (x1,x2,…,xn)∈A(n,0).
* If xn+1=2 or 5 then (x1,x2,…,xn)∈A(n,1).
* If xn+1=1 then (x1,x2,…,xn)∈A(n,2).
Hence, an+1=an+2bn+cn (1). Similarly, we get
bn+1=an+bn+2cn,(2)
cn+1=2an+bn+cn.(3)
From those equations, we have a2=5, b2=6, c2=5, a3=22, b3=21, c3=21. Moreover,
an+1−bn+1=an+2bn+cn−an−bn−2cn=bn−cn,
bn+1−cn+1=an+bn+2cn−2an−bn−cn=cn−an,
cn+1−an+1=cn+1−bn+1+bn+1−an+1=an−bn.
This leads to
an+3−bn+3=bn+2−cn+2=cn+1−an+1=an−bn.
Similarly,
bn+3−cn+3=bn−cn,cn+3−an+3=cn−an.
Hence, it is easy to see that
* If k≡0(mod3) then bk=ck=ak−1.
* If k≡1(mod3) then ak=bk=ck−1.
* If k≡2(mod3) then ak=ck=bk−1.
On the other hand, ak+bk+ck is equal to the cardinal number of A(k)={akak−1…a1:aj∈S} so that
ak+bk+ck=4k.
In conclusion, the value of ak is
* ak=34k−1 if k is not a multiple of 3;
* ak=34k+2 if k is a multiple of 3.