fork(1) download
  1. ; lychrel numbers
  2.  
  3. (define (r n)
  4. (let loop ((n n) (z 0))
  5. (if (zero? n) z
  6. (loop (quotient n 10)
  7. (+ (* z 10) (modulo n 10))))))
  8.  
  9. (define (s n) (+ n (r n)))
  10.  
  11. (define (p? n) (= n (r n)))
  12.  
  13. (define (lychrel n . b)
  14. (let ((b (if (null? b) 100 (car b))))
  15. (let loop ((b b) (z n) (xs (list n)))
  16. (if (zero? b) (list n #f)
  17. (let ((x (s z)))
  18. (if (p? x) (reverse (cons x xs))
  19. (loop (- b 1) x (cons x xs))))))))
  20.  
  21. (display (lychrel 281)) (newline)
  22. (display (lychrel 196)) (newline)
  23.  
  24. (do ((n 1 (+ n 1))) ((< 1000 n))
  25. (when (not (cadr (lychrel n)))
  26. (display n) (newline)))
Success #stdin #stdout 2.91s 10672KB
stdin
Standard input is empty
stdout
(281 463 827 1555 7106 13123 45254)
(196 #f)
196
295
394
493
592
689
691
788
790
879
887
978
986