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.

A005185 Hofstadter Q-sequence: a(1) = a(2) = 1; a(n) = a(n-a(n-1)) + a(n-a(n-2)) for n > 2.

Original entry on oeis.org

1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 8, 8, 8, 10, 9, 10, 11, 11, 12, 12, 12, 12, 16, 14, 14, 16, 16, 16, 16, 20, 17, 17, 20, 21, 19, 20, 22, 21, 22, 23, 23, 24, 24, 24, 24, 24, 32, 24, 25, 30, 28, 26, 30, 30, 28, 32, 30, 32, 32, 32, 32, 40, 33, 31, 38, 35, 33, 39, 40, 37, 38, 40, 39
Offset: 1

Views

Author

Simon Plouffe and N. J. A. Sloane, May 20 1991

Keywords

Comments

Rate of growth is not known. In fact it is not even known if this sequence is defined for all positive n.
Roman Pearce, Aug 29 2014, has computed that a(n) exists for n <= 10^10. - N. J. A. Sloane
a(n) exists for n <= 3*10^10. - M. Eric Carr, Jul 02 2023

Examples

			a(18) = 11 because a(17) is 10 and a(16) is 9, so we take a(18 - 10) + a(18 - 9) = a(8) + a(9) = 5 + 6 = 11.
		

References

  • B. W. Conolly, "Meta-Fibonacci sequences," in S. Vajda, editor, Fibonacci and Lucas Numbers and the Golden Section. Halstead Press, NY, 1989, pp. 127-138.
  • R. K. Guy, Unsolved Problems in Number Theory, Sect. E31.
  • D. R. Hofstadter, Goedel, Escher, Bach: an Eternal Golden Braid, Random House, 1980, p. 138.
  • N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).
  • S. Vajda, Fibonacci and Lucas Numbers and the Golden Section, Wiley, 1989, see p. 129.
  • S. Wolfram, A New Kind of Science, Wolfram Media, 2002; p. 129.

Crossrefs

Cf. A081827 (first differences).
Cf. A226244, A226245 (record values and where they occur).
See A244477 for a different start.

Programs

  • C
    #include 
    #define LIM 20
    int Qa[LIM];
    int Q(int n){if (n==1 || n==2){return 1;} else{return Qa[n-Qa[n-1]]+Qa[n-Qa[n-2]];}}
    int main(){int i;printf("n\tQ\n");for(i=1; iGonzalo Ciruelos, Aug 01 2013
    
  • Haskell
    a005185 n = a005185_list !! (n-1)
    a005185_list = 1 : 1 : zipWith (+)
       (map a005185 $ zipWith (-) [3..] a005185_list)
       (map a005185 $ zipWith (-) [3..] $ tail a005185_list)
    -- Reinhard Zumkeller, Jun 02 2013, Sep 15 2011
    
  • Magma
    I:=[1,1]; [n le 2 select I[n] else Self(n-Self(n-1))+Self(n-Self(n-2)): n in [1..90]]; // Vincenzo Librandi, Aug 08 2014
    
  • Maple
    A005185 := proc(n) option remember;
        if n<=2 then 1
        elif n > procname(n-1) and n > procname(n-2) then
            RETURN(procname(n-procname(n-1))+procname(n-procname(n-2)));
        else
            ERROR(" died at n= ", n);
        fi; end proc;
    # More generally, the following defines the Hofstadter-Huber sequence Q(r,s) - N. J. A. Sloane, Apr 15 2014
    r:=1; s:=2;
    a:=proc(n) option remember; global r,s;
    if n <= s then  1
    else
        if (a(n-r) <= n) and (a(n-s) <= n) then
        a(n-a(n-r))+a(n-a(n-s));
        else lprint("died with n =",n); return (-1);
        fi; fi; end;
    [seq(a(n), n=1..100)];
  • Mathematica
    a[1]=a[2]=1; a[n_]:= a[n]= a[n -a[n-1]] + a[n -a[n-2]]; Table[ a[n], {n,70}]
  • MuPAD
    q:=proc(n) option remember; begin if n<=2 then 1 else q(n-q(n-1))+q(n-q(n-2)) end_if; end_proc: q(i)$i=1..100; // Zerinvary Lajos, Apr 03 2007
    
  • PARI
    {a(n)= local(A); if(n<1, 0, A=vector(n,k,1); for(k=3, n, A[k]= A[k-A[k-1]]+ A[k-A[k-2]]); A[n])} /* Michael Somos, Jul 16 2007 */
    
  • Python
    from functools import lru_cache
    @lru_cache(maxsize=None)
    def a(n):
        if n < 3: return 1
        return a(n - a(n-1)) + a(n - a(n-2))
    print([a(n) for n in range(1, 75)]) # Michael S. Branicky, Jul 26 2021
  • Sage
    @CachedFunction
    def a(n):
        if (n<3): return 1
        else: return a(n -a(n-1)) + a(n -a(n-2))
    [a(n) for n in (1..70)] # G. C. Greubel, Feb 13 2020
    
  • Scheme
    (define q (lambda (n) (cond ( (eqv? n 0) 1) ( (eqv? n 1) 1) ( #t (+ (q (- n (q (- n 1)))) (q (- n (q (- n 2)))))))))
    
  • Scheme
    ;; An implementation of memoization-macro definec can be found for example in: http://oeis.org/wiki/Memoization
    (definec (A005185 n) (if (<= n 2) 1 (+ (A005185 (- n (A005185 (- n 1)))) (A005185 (- n (A005185 (- n 2)))))))
    ;; Antti Karttunen, Mar 22 2017