cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

A333596 a(0) = 0; for n > 0, a(n) = a(n-1) + (number of 1's and 3's in base-4 representation of n) - (number of 0's and 2's in base-4 representation of n).

Original entry on oeis.org

0, 1, 0, 1, 1, 3, 3, 5, 3, 3, 1, 1, 1, 3, 3, 5, 4, 5, 4, 5, 6, 9, 10, 13, 12, 13, 12, 13, 14, 17, 18, 21, 18, 17, 14, 13, 12, 13, 12, 13, 10, 9, 6, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6, 9, 10, 13, 12, 13, 12, 13, 14, 17, 18, 21, 19, 19, 17, 17, 17, 19, 19, 21, 19, 19
Offset: 0

Views

Author

Keywords

Comments

Local maxima values minus 1 are divisible by 4.
For a digit-wise recurrence, it's convenient to sum n terms so b(n) = a(n-1) = Sum_{i=0..n-1} A334841(i). Then b(4n+r) = 4*b(n) + r*A334841(n) + (1 if r even), for 0 <= r <= 3 and 4n+r >= 1. This is 4 copies of terms 0..n-1 and r copies of the following n. The new lowest digits cancel when r is odd, or net +1 when r is even. Repeatedly expanding gives the PARI code below. - Kevin Ryde, Jun 02 2020

Examples

			      n in    #odd    #even
  n  base 4  digits - digits + a(n-1) = a(n)
  =  ======  ===============================
  0    0        0   -                     0
  1    1        1   -    0   +    0   =   1
  2    2        0   -    1   +    1   =   0
  3    3        1   -    0   +    0   =   1
  4   10        1   -    1   +    1   =   1
  5   11        2   -    0   +    1   =   3
  6   12        1   -    1   +    3   =   3
  7   13        2   -    0   +    3   =   5
		

Crossrefs

Programs

  • Maple
    a:= proc(n) option remember; `if`(n=0, 0, a(n-1) +add(
         `if`(i in [1, 3], 1, -1), i=convert(n, base, 4)))
        end:
    seq(a(n), n=0..80);  # Alois P. Heinz, May 30 2020
  • Mathematica
    f[n_] := Total[(-1)^(r = Range[0, 3]) * DigitCount[n, 4, r]]; a[0] = 0; a[n_] := a[n] = a[n - 1] - f[n]; Array[a, 100, 0] (* Amiram Eldar, Apr 24 2020 *)
  • PARI
    a(n) = my(v=digits(n+1,4),s=0); for(i=1,#v, my(t=v[i]); v[i]=t*s+!(t%2); s-=(-1)^t); fromdigits(v,4); \\ Kevin Ryde, May 30 2020
    
  • PARI
    b(n)=my(d=digits(n,4)); -sum(i=1,#d,(-1)^d[i])
    first(n)=my(s); concat(0,vector(n,k,s+=b(k))) \\ Charles R Greathouse IV, Jul 04 2020
    
  • Python
    import numpy as np
    def qnary(n):
        e = n//4
        q = n%4
        if n == 0 : return 0
        if e == 0 : return q
        if e != 0 : return np.append(qnary(e), q)
    m = 400
    v = [0]
    for i in range(1,m+1) :
        t = np.array(qnary(i))
        t[t%2 != 0] = 1
        t[t%2 == 0] = -1
        v = np.append(v, np.sum([np.sum(t), v[i-1]]))
    
  • Python
    from itertools import accumulate
    def A334841(n):
        return 2*bin(n)[-1:1:-2].count('1')-(len(bin(n))-1)//2 if n > 0 else 0
    A333596_list = list(accumulate(A334841(n) for n in range(10000))) # Chai Wah Wu, Sep 03 2020
  • R
    qnary = function(n, e, q){
      e = floor(n/4)
      q = n%%4
      if(n == 0 ){return(0)}
      if(e == 0){return(q)}
      else{return(c(qnary(e), (q)))}
    }
    m = 400
    s = seq(2,m)
    v = c(0)
    for(i in s){
      x = qnary(i-1)
      x[which(x%%2!=0)] = 1
      x[which(x%%2==0)] = -1
      v[i] = sum(x,v[i-1])
    }