;;
;; Here is Scheme-code for A257676, better indented and commented, although
;; otherwise same as in entry https://oeis.org/A257676
;;
;; For macros like defineperm1, see https://github.com/karttu/IntSeq
;;
;; For any other missing functions (which are to be published in their due time!),
;; please mail me at @gmail.com
;;
;; Best regards, Antti Karttunen, May 4, 2015.
;;
;; Bear in mind that the left hand child of n, A213723(n), if it exists (i.e. when n is not in A055938)
;; is always even, and the right hand child (A213724(n), if it exists), is always odd,
;; with A213724(n) = A213723(n)+1, whenever A213723(n) is not zero.
(defineperm1 (A257676 n)
(if (<= n 1)
n
(let ((prev (A257676 (- n 1))))
(cond ((= 1 (A213719 prev)) ;; If the previous term is in the infinite trunk of binary beanstalk?
(if (zero? (A213719 (A213723 prev))) ;; And the node to the left is not...
(A213723 prev) ;; Then choose it.
(A213724 prev) ;; Otherwise choose the right hand child.
)
)
((not (zero? (A213723 prev))) (A213723 prev)) ;; If we can go left in this tendril, then let's go...
((not (zero? (A213724 prev))) (A213724 prev)) ;; If we can go right in this tendril, then let's go...
(else ;; Otherwise, we have to backtrack in the beanstalk-tree.
(let loop ((prev prev) (back (A011371 prev)))
(cond ((= 1 (A213719 back)) ;; We got all the way back to the trunk?
(if (zero? (A213719 (A213723 back))) ;; And the node to the left of it is not there
(A213724 back) ;; Then choose the other (rhs) branch, to proceed up the trunk
(A213723 back) ;; Otherwise the trunk proceeds to the left, go there.
)
)
((and (even? prev) (not (zero? (A213724 back)))) ;; Climbing down from even...
(A213724 back) ;; and there's a way to the right, so let's go there!
)
(else (loop back (A011371 back))) ;; Otherwise, keep on climbing down towards the root.
)
)
)
)
)
)
)
(define (A257677 n) (A257676 (- n))) ;; The magic kludge provided by defineperm1-macro
(define A257678 (FIXED-POINTS 1 0 A257676))