Skip to content

Instantly share code, notes, and snippets.

@plaster
Created December 10, 2012 13:49
Show Gist options
  • Save plaster/4250659 to your computer and use it in GitHub Desktop.
Save plaster/4250659 to your computer and use it in GitHub Desktop.
;; 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)))
@kohyama
Copy link

kohyama commented Dec 11, 2012

いずれの関数も意図通りの動作をしているようです. 参考になります.

この factorize は, 素因数を必ず降順で返すので, reduce max じゃなくて first でも良かったり.
(もしそうするなら, 関数名か doc-string で降順であることを明示した上で, の方が良いとは思います.)

@tnoda
Copy link

tnoda commented Dec 12, 2012

👍

@plaster
Copy link
Author

plaster commented Dec 12, 2012

反映しました。ありがとうございます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment