Created
December 10, 2012 13:49
-
-
Save plaster/4250659 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; seq-end-inclusive を割れそうな数の候補 | |
(defn denominators [seq-end-inclusive] | |
(for [d (range 2 (+ seq-end-inclusive 1)) | |
:while (<= (* d d) seq-end-inclusive) | |
] | |
d)) | |
;; end-inclusive 以下の素数列をエラトステネスの篩で | |
(defn gen-primes [end-inclusive] | |
(reduce (fn [primes d] (filter #(or (= %1 d) | |
(< 0 (mod %1 d))) | |
primes)) | |
(range 2 (+ end-inclusive 1)) | |
(denominators end-inclusive))) | |
;; large-number を素因数分解する | |
(defn factorize | |
"returns sequence of factors in descending order" | |
[large-number] | |
(let [primes (->> large-number | |
Math/sqrt Math/ceil long | |
gen-primes) | |
] | |
(loop [large-number large-number | |
result nil | |
[p & next-primes :as primes] primes | |
] | |
(cond | |
(= 1 large-number) result | |
(nil? primes) (cons large-number result) | |
(= 0 (mod large-number p) | |
) (recur (quot large-number p) (cons p result) primes) | |
true (recur large-number result next-primes) | |
)))) | |
;; 一番でっかい素因数 | |
(defn solve [large-number] | |
(first (factorize large-number))) |
👍
反映しました。ありがとうございます。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
いずれの関数も意図通りの動作をしているようです. 参考になります.
この
factorize
は, 素因数を必ず降順で返すので,reduce max
じゃなくてfirst
でも良かったり.(もしそうするなら, 関数名か doc-string で降順であることを明示した上で, の方が良いとは思います.)