[ prog / sol / mona ]

prog


LISP Puzzles

91 2020-05-16 22:41

>>90

I was blind but now I see, what an elegant sequence.

(define (hofstadter n)
  (letrec ((H (lambda (i R S)
               (cond ((zero? i) (car R))
                     ((member S R)
                      (H (- i 1) (cons (+ (car R) S 1) R) (+ S 2)))
                     (else (H (- i 1) (cons (+ (car R) S) R) (+ S 1)))))))
    (H (sub1 n) '(1) 2)))

(map (lambda (e) (hofstadter (expt 10 e))) '(0 1 2 3 4))
=> (1 69 5764 526334 50874761)
;; (1 69 5764 526334 50874761)
157


VIP:

do not edit these