Created
April 26, 2012 08:21
project euler 80
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Problem 80 : 2012/4/26 | |
;;"Elapsed time: 324.264961 msecs" | |
(require '[clojure.math.numeric-tower :as math]) | |
(defn square-num? [n] | |
(= (math/expt (int (Math/sqrt n)) 2) n)) | |
(defn num-cat [[u l]] | |
(+ (* u 10) l)) | |
(defn biggest-x [bring-down tgt] | |
(last (take-while #(< (second %) tgt) | |
(map #(vector % (* % (num-cat [bring-down %]))) | |
(range 0 10))))) | |
(defn extract-sqrt [num & {:keys [p] :or {p 100}}] | |
(loop [root [], | |
tgt-digit num, | |
bring-down-s 0N] | |
(let [[x n-mul] (biggest-x bring-down-s tgt-digit) | |
new-root (conj root x)] | |
(if (>= (count new-root) p) | |
new-root | |
(recur new-root | |
(* (- tgt-digit n-mul) 100) | |
(+ (num-cat [bring-down-s x]) x)))))) | |
(defn pe80 [limit] | |
(reduce + | |
(map #(reduce + (extract-sqrt %)) | |
(filter (complement square-num?) | |
(range 2 (inc limit)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment