Created
December 15, 2012 09:08
-
-
Save emanon001/4292285 to your computer and use it in GitHub Desktop.
Project Euler Problem 6 #mitori_clj
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
;;; Project Euler Problem 6 | |
;;; 「二乗の和と和の二乗の差はいくつか?」 | |
;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%206 | |
(use 'clojure.test) | |
(defn sum-of-squares | |
"与えられた数列についてその二乗の和を返します" | |
[nums] | |
(apply + (map #(* % %) nums))) | |
(defn square-of-sum | |
"与えられた数列についてその和の二乗を返します" | |
[nums] | |
(apply * (repeat 2 (apply + nums)))) | |
(defn solve | |
"最初の n 個の自然数について、二乗の和と和の二乗の差を返します" | |
[n] | |
(->> (range 1 (inc n)) | |
((juxt square-of-sum sum-of-squares)) | |
(apply -))) | |
(is (= (solve 10) 2640)) | |
(time (solve 100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
juxt
は面白い関数なのですが、使うのにちょうど良い場面になかなか遭遇しない(ぼくが気づいていない)ですね。うまく使えている例だと思います。