#C18.txt, March 28, 2016 Hofstadter sequences Help:=proc(): print(` Q(n) , SeqQ(N), Qg(n), SeqQg(N), NF(n)`): end: #Q(n): the n-th term of the ORIGINAL Nathaniel Shar' uncle Q-sequence Q:=proc(n) option remember: if n=FAIL or n<=0 then RETURN(FAIL): elif n=1 or n=2 then RETURN(1): else RETURN(Q(n-Q(n-1))+Q(n-Q(n-2)) ): fi: end: #The firt N terms of the D. H. sequence SeqQ:=proc(N) local n: [seq(Q(n),n=1..N)]: end: Qg:=proc(n,INI) option remember: if n=FAIL or n<=0 then RETURN(FAIL): elif n<=nops(INI) then RETURN( INI[n] ): else RETURN(Qg(n-Qg(n-1,INI), INI)+Qg(n-Qg(n-2,INI), INI) ): fi: end: #The firt N terms of the D. H. sequence SeqQg:=proc(N,INI) local n: [seq(Qg(n, INI),n=1..N)]: end: #QgZ(n,INI): the Doug sequence with general initial conditions INI #and the convention of Ruskey and Nathan Fox that if the argument #is <=0 you don't die, but you get 0 QgZ:=proc(n,INI) option remember: if n=FAIL then RETURN(FAIL): elif n<=0 then RETURN(0): elif n<=nops(INI) then RETURN( INI[n] ): elif QgZ(n-1,INI)=FAIL or QgZ(n-2,INI)=FAIL or QgZ(n-1,INI)<=0 or QgZ(n-2,INI)<=0 then RETURN(FAIL): else RETURN(QgZ(n-QgZ(n-1,INI), INI)+QgZ(n-QgZ(n-2,INI), INI) ): fi: end: #The firt N terms of the D. H. sequence SeqQgZ:=proc(N,INI) local n: [seq(QgZ(n, INI),n=1..N)]: end: #a(1) = 4, a(2) = 0; thereafter a(6n) = 6n, #a(6n+1) = 6, a(6n+2) = 3, a(6n+3) = 3n^2+3n+5, a(6n+4) = 6, a(6n+5) = 2 #NF(n): the n-th term using Nathan Fox's computer's formula from OEIS A264757 #using the Maple program written by Nathan Fox NF:=proc(n): if n=1 then 4: elif n=2 then 0: elif n mod 6=0 then n: elif n mod 3=1 then 6: elif n mod 6=2 then 3: elif n mod 6=3 then 3*((n-3)/6)^2+(n-3)/2+5: elif n mod 6=5 then 2: else print(`MathIsFun`): fi: end: