Skip to content

SternDiatomicSequence ​

Stern's diatomic sequence (the fusc function) 0,1,1,2,1,3,2,3,…0, 1, 1, 2, 1, 3, 2, 3, … as a lazy indexed collection.

SternDiatomicSequences(0)=0s(0) = 0, s(1)=1s(1) = 1, s(2n)=s(n)s(2n) = s(n), s(2n+1)=s(n)+s(n+1)s(2n+1) = s(n) + s(n+1), an infinite indexed collection.

Domain: Collections

Details
  • A lazy indexed collection: Count(SternDiatomicSequence)=+∞Count(SternDiatomicSequence) = +\infty, and At(SternDiatomicSequence,1)=s(0)=0At(SternDiatomicSequence, 1) = s(0) = 0; unranking is O(log⁡k)O(\log k) via the bit-doubling pair (s(n),s(n+1))(s(n), s(n+1)), not a growing cache.
  • OEIS A002487, starting exactly at its offset-0 term: 0,1,1,2,1,3,2,3,1,4,…0, 1, 1, 2, 1, 3, 2, 3, 1, 4, ….
  • Not monotone, so membership is not 'is this term ≤\le some bound': every non-negative integer is a term (00 appears exactly once, at n=0n = 0; every positive integer appears infinitely often). Element(0,SternDiatomicSequence)Element(0, SternDiatomicSequence) and Element(42,SternDiatomicSequence)Element(42, SternDiatomicSequence) are both true; only a negative or non-integer value is false.

Definition

Epsil, as the implementation runs it.

count()

PositiveInfinity

unrank(_r)

Fold(Function(
               [(st_p[1] - st_p[1] % 2) / 2, st_p[2] + st_p[3], st_p[3]] if st_p[1] %
                                                                            2 ==
                                                                            0 else [
                                                                                    (st_p[1] -
                                                                                     st_p[1] % 2) /
                                                                                    2,
                                                                                    st_p[2],
                                                                                    st_p[2] + st_p[3],
                                                                                  ],
               st_p,
               st_b,
             ), [_r, 1, 0], Range(1, Floor(Log(_r + 1, 2)) + 2, 1))[3]

rank(_x)

(-1 if _x < 0 else _x) if _x < 2 else Fold(
                                            Function(Apply(
                                                            (st_e: list<integer>) => 2 ^ st_e[4] +
                                                                                     st_e[3] if GCD(_x, st_w) ==
                                                                                         1 &&
                                                                                         2 ^ st_e[4] +
                                                                                         st_e[3] <
                                                                                         st_best else st_best,
                                                            Fold(
                                                                  Function(st_s if st_s[1] == st_s[2] else [
                                                                                                             st_s[1] -
                                                                                                             (st_s[1] - 1 -
                                                                                                              (st_s[1] - 1) %
                                                                                                              st_s[2]) /
                                                                                                             st_s[2] *
                                                                                                             st_s[2],
                                                                                                             st_s[2],
                                                                                                             st_s[3] +
                                                                                                             (2 ^
                                                                                                              ((st_s[1] - 1 -
                                                                                                                (st_s[1] - 1) %
                                                                                                                st_s[2]) /
                                                                                                               st_s[2]) -
                                                                                                              1) *
                                                                                                             2 ^ st_s[4],
                                                                                                             st_s[4] +
                                                                                                             (st_s[1] - 1 -
                                                                                                              (st_s[1] - 1) %
                                                                                                              st_s[2]) /
                                                                                                             st_s[2],
                                                                                                           ] if st_s[1] > st_s[2] else [
                                                                                                                                        st_s[1],
                                                                                                                                        st_s[2] -
                                                                                                                                        (st_s[2] - 1 -
                                                                                                                                         (st_s[2] - 1) %
                                                                                                                                         st_s[1]) /
                                                                                                                                        st_s[1] *
                                                                                                                                        st_s[1],
                                                                                                                                        st_s[3],
                                                                                                                                        st_s[4] +
                                                                                                                                        (st_s[2] - 1 -
                                                                                                                                         (st_s[2] - 1) %
                                                                                                                                         st_s[1]) /
                                                                                                                                        st_s[1],
                                                                                                                                      ], st_s, st_k),
                                                                  [
                                                                    _x,
                                                                    st_w,
                                                                    0,
                                                                    0,
                                                                  ],
                                                                  Range(
                                                                         1,
                                                                         2 *
                                                                         Floor(Log(_x, 2)) + 4,
                                                                         1,
                                                                       ),
                                                                ),
                                                          ), st_best, st_w),
                                            PositiveInfinity,
                                            Range(1, _x - 1, 1),
                                          )

valid(_x)

_x >= 0

Enumeration

Examples

1 more kept as data, checked against the oracles
About
Examples1
CrosswalkOEIS

See also: Count, At, Element, ThueMorseNumbers