# zerodelta's solution

## to Difference Of Squares in the Clojure Track

Published at Nov 01 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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.

## Source

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

## Submitting Incomplete Solutions

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

### difference_of_squares_test.clj

``````(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)

(defn- naturals [n]
(->> (range)
(map inc)
(take n)))

(defn sum-of-squares [n]
(reduce + (map #(* % %) (naturals n))))

(defn square-of-sums [n]
(let [sum (reduce + (naturals n))]
(* sum sum)))

(defn difference [n]
(- (square-of-sums n) (sum-of-squares n)))``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

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?