restart: n := 5: # How many terms of the orbits. # eight_forward is the forward orbit of g^(k)(8); # eight_backward the backward orbit. g := proc(n:posint) local m; m := modp(n,3); if m = 0 then 2*n/3; elif m=1 then (4*n-1)/3; else (4*n+1)/3; fi; end: h := proc(n:posint) local m; m := modp(n,4); if m = 1 then (3*n+1)/4; elif m=3 then (3*n-1)/4; else 3*n/2; fi; end: eight_forward := proc(n:posint) local m,r; r := [8]; for m from 1 to n do r := [op(r),g(r[nops(r)])]; od; r; end: eight_backward := proc(n:posint) local m,r; r := [8]; for m from 1 to n do r := [op(r),h(r[nops(r)])]; od; r; end: ``(eight_forward)(n) = eight_forward(n); ``(eight_backward)(n) = eight_backward(n);