Project Euler

Problem #6

The sum of the squares of the first ten natural numbers is,

1^(2) + 2^(2) + ... + 10^(2) = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^(2) = 55^(2) = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Clojure: Running time = 1.76s
+;pow

(defn p6 []
  (println
    (- (pow (apply + (range 1 101)) 2) (apply + (map #(pow % 2) (range 1 101))))
  )
)

Erlang: Running time = 0.14s
+%pow

p6()->io:format("~w~n",[pow(lists:sum(lists:seq(1,100)),2)-lists:sum(lists:map(fun(X)->X*X end, lists:seq(1,100)))]).

Ruby: Running time = 0.02s
+#Enumerable

def p6
  puts (1..100).sum**2-(1..100).map{|i|i**2}.sum
end

Scala: Running time = 0.18s
+//pow

def p6{
  val r=(1 to 100)
  println(pow(r.foldLeft(0)(_+_),2)-r.map(pow(_,2).intValue).foldLeft(0)(_+_))
}