Question
The sum of the squares of the first ten natural numbers is,
The square of the sum of the first ten natural numbers is,
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is .
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Clojure
#!/usr/bin/env clojure
(defn square [x]
(* x x))
(defn sum-squares [limit]
(apply + (map square (range 1 (+ limit 1)))))
(defn square-sum [limit]
(square (apply + (range 1 (+ limit 1)))))
(println (- (square-sum 100) (sum-squares 100)))
Go
package main
import "fmt"
func main() {
sumSquares, squareSum := 0, 0
for i := 1; i <= 100; i++ {
sumSquares += i * i
squareSum += i
}
squareSum *= squareSum
fmt.Println(squareSum - sumSquares)
}
Haskell
JavaScript
let sum = 0, sumSquares = 0
for (let i = 1; i <= 100; i++) {
sum += i
sumSquares += i * i
}
console.log(sum * sum - sumSquares)
Python
Ruby
#!/usr/bin/env ruby
puts ((1..100).inject(0) {|s,v| s += v})**2 - ((1..100).collect {|x| x**2}.inject(0) { |s,v| s += v})