Project Euler Problem 6

Ivar Thorson bio photo By Ivar Thorson

Not much to say about this problem. Everyone’s solution seemed similar to my own, with some minor stylistic variation.

(def natural-numbers (iterate inc 1))

(defn square-of-sum [coll]
  (expt (reduce + coll) 2))

(defn sum-of-squares [coll]
  (reduce + (map #(expt % 2) coll)))

(defn diff-between-squares [coll]
  (- (square-of-sum coll) (sum-of-squares coll)))

(diff-between-squares (take 100 natural-numbers))

Yes, the definition of natural-numbers is perhaps a bit pedantic, but thought it might be fun to try to ensure I didn’t accidentally take a list of numbers starting at 0 and ending at 99…although technically this might be considered a set of natural numbers as well, according to wikipedia.