Skip to content

Instantly share code, notes, and snippets.

@ypsilon-takai
Last active September 28, 2015 10:09
Show Gist options
  • Select an option

  • Save ypsilon-takai/1422420 to your computer and use it in GitHub Desktop.

Select an option

Save ypsilon-takai/1422420 to your computer and use it in GitHub Desktop.
project euler 69
;; 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))))))
@ypsilon-takai
Copy link
Copy Markdown
Author

アクセスがあったので、ちらっと見てみたら、prodect-list が (reductions * ) と同じ処理であることに気づいたので、作りなおした。
必要な素数列作成コスト込みで1msec以下。

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