これ読んでたら Clojure で実装したくなったのでやってみた*1。 最終的なコードはこれ。 (ns prime-factorization.core (:gen-class)) (def prime-numbers ((fn f [x] (cons x (lazy-seq (f (first (drop-while (fn [n] (some #(zero? (mod n %)) (take-while #(<= (* % %) n) prime-numbers))) (iterate inc (inc x)))))))) 2)) (defn prime-factorization [n] (if (< n 2) [n] (loop [dividend n prime-factors [] primes (take-while #(<= % (Math/sqrt n)) prime-n
![Clojure での素因数分解プログラム - (define -ayalog '())](https://cdn-ak-scissors.b.st-hatena.com/image/square/8b3937403f59aa3a0ec51a344e4222e53cc0cc88/height=288;version=1;width=512/http%3A%2F%2Fcdn-ak.f.st-hatena.com%2Fimages%2Ffotolife%2Fa%2Fayato0211%2F20150415%2F20150415124305.png)