Published at Oct 15 2018
·
0 comments

Instructions

Test suite

Solution

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

The square of the sum of the first ten natural numbers is (1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is 1² + 2² + ... + 10² = 385.

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

Problem 6 at Project Euler http://projecteuler.net/problem=6

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
(ns difference-of-squares-test
(:require [clojure.test :refer [deftest is]]
[difference-of-squares :as dos]))
(deftest square-of-sums-to-5
(is (= 225 (dos/square-of-sums 5))))
(deftest sum-of-squares-to-5
(is (= 55 (dos/sum-of-squares 5))))
(deftest difference-of-sums-to-5
(is (= 170 (dos/difference 5))))
(deftest square-of-sums-to-10
(is (= 3025 (dos/square-of-sums 10))))
(deftest sum-of-squares-to-10
(is (= 385 (dos/sum-of-squares 10))))
(deftest difference-of-sums-to-10
(is (= 2640 (dos/difference 10))))
(deftest square-of-sums-to-100
(is (= 25502500 (dos/square-of-sums 100))))
(deftest sum-of-squares-to-100
(is (= 338350 (dos/sum-of-squares 100))))
(deftest difference-of-sums-to-100
(is (= 25164150 (dos/difference 100))))
```

```
(ns difference-of-squares)
(def square #(* % %))
(def up-to (comp range inc))
(defn sum-of-squares [number]
(->> number up-to (map square) (apply +)))
(defn square-of-sums [number]
(->> number up-to (apply +) square))
(defn difference [number]
(apply - (map #(% number) [square-of-sums sum-of-squares])))
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

## Community comments