Last active
September 28, 2015 10:09
-
-
Save ypsilon-takai/1422420 to your computer and use it in GitHub Desktop.
project euler 69
This file contains hidden or 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
| ;; Problem 69 : 2011/12/6 | |
| ;; This needs too long time. | |
| ;; "Elapsed time: 980693.545995 msecs" | |
| (defn get-n-slash-phi-n [n] | |
| (/ 1 (reduce * (map #(- 1 (/ 1 %)) (distinct (factors n)))))) | |
| (defn pe-69 [end-val] | |
| (reduce #(if (> (first %1) (first %2)) %1 %2) | |
| (pmap #(vector (get-n-slash-phi-n %) %) (range 2 (inc end-val))))) | |
| ;; Another implementation. | |
| ;; "Elapsed time: 0.225784 msecs" | |
| (defn product-list | |
| ([col] (product-list (rest col) (first col))) | |
| ([col p] | |
| (if (empty? col) | |
| (list p) | |
| (lazy-seq | |
| (cons p | |
| (product-list (next col) (* p (first col)))))))) | |
| (defn pe-69 [n] | |
| (let [max-prime-product (last | |
| (take-while #(< % n) | |
| (product-list (get-prime-list))))] | |
| (take-while #(< % n) | |
| (map #(* max-prime-product %) (iterate inc 1))))) | |
| ;; 2013/1/11 | |
| ;; finaly found that product-list do exactly same as (reductions *) | |
| ;; this is my final (really?) answer contains only 1 function. | |
| ;; "Elapsed time: 0.654412 msecs" includes prime sequence generation time. | |
| (defn pe-69 [n] | |
| (let [max-prime-product | |
| (last | |
| (take-while #(< % n) | |
| (reductions * (create-primes (make-is-prime?)))))] | |
| (last | |
| (take-while #(< % n) | |
| (map #(* max-prime-product %) (iterate inc 1)))))) | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
アクセスがあったので、ちらっと見てみたら、prodect-list が (reductions * ) と同じ処理であることに気づいたので、作りなおした。
必要な素数列作成コスト込みで1msec以下。