Gaucheで素数の二進法表記
format "~8@a" で右詰めにして、trで"01"を":#"に変換する。
trの呼び出しを1回にしているのがミソ。
(use srfi-1) (use srfi-42) (use text.tr) (define (primes n) (cond ((<= n 1) (list)) ((= n 2) (list 2)) (else (let loop ((l (list-ec (:range x 3 n 2) x)) (prime-list (list 2))) (let1 m (car l) (if (> (expt m 2) n) (append! (reverse! prime-list) l) (loop (remove! (lambda (x) (zero? (modulo x m))) l) (cons m prime-list)))))))) (print (with-string-io (string-join (map (lambda (x) (format "~8@a" (number->string x 2))) (primes 100)) "\n") (cut tr "01" ":#")))
#: ## #:# ### #:## ##:# #:::# #::## #:### ###:# ##### #::#:# #:#::# #:#:## #:#### ##:#:# ###:## ####:# #::::## #:::### #::#::# #::#### #:#::## #:##::# ##::::#