Last active
March 28, 2018 13:23
-
-
Save katsuyan/b183f08b945e4ba850809da6949f38f1 to your computer and use it in GitHub Desktop.
数学パズルonClojure-Q18
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
(ns puzzule.q18) | |
(defn check [last_n used lst] | |
(let [now_lst (get lst last_n)] | |
(if (and (every? #(= true %) used) (.contains (vec (get lst 1)) last_n)) | |
[1] | |
(loop [i (first now_lst) now_lst_recur (rest now_lst)] | |
(if (nil? i) | |
[] | |
(let [i-1 (- i 1)] | |
(if (= (get used i-1) false) | |
(let [result (check i (assoc used i-1 true) lst)] | |
(if (not (empty? result)) | |
(concat [i] result) | |
(recur (first now_lst_recur) (rest now_lst_recur)))) | |
(recur (first now_lst_recur) (rest now_lst_recur))))))))) | |
(defn square [n] | |
(map #(Math/pow % 2) (range 2 (+ 1 (int (Math/sqrt (* n 2))))))) | |
(defn candidate-list [n i lst] | |
(if (< i 1) | |
lst | |
(recur n (dec i) (assoc lst i (filter #(> % 0) (map #(- % i) (map int (square n)))))))) | |
(loop [i 2] | |
(let [cake (check 1 (vec (cons true (take (dec i) (repeat false)))) (candidate-list i i {}))] | |
(if (empty? cake) | |
(recur (inc i)) | |
i))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment