Arantium Maestum

プログラミング、囲碁、読書の話題

Clojure入門 - Project Eulerを解いてみる 問6

第六問

和の二乗と二乗の和の差を求める問題。

以下ネタバレ

(defn square [x]
  (* x x))

(defn sum-square [coll]
  (apply +
    (map square coll)))

(defn square-sum [coll]
  (square
    (apply + coll)))

(defn ss-ss [coll]
  (- (square-sum coll) (sum-square coll)))

(println (ss-ss (range 1 101)))

特に言うべきこともない、まさにそのまんまなコードである。

強いて言うなら(range 1 101)の部分が少し気になるかもしれない。変数化して(range 1 (inc n))にするか、あるいは(map inc (range n))にするか。