ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Nov 04 2020
·
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.

You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged. Finding the best algorithm for the problem is a key skill in software engineering.

To compile and run the tests, just run the following in your exercise directory:

```
$ nim c -r test_difference_of_squares.nim
```

Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/nim/difference-of-squares`

directory.

You can find your Exercism workspace by running `exercism debug`

and looking for the line that starts with `Exercises Directory`

.

These guides should help you,

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.

```
import unittest
import difference_of_squares
suite "Difference of Squares":
test "square of sum 1":
check squareOfSum(1) == 1
test "square of sum 5":
check squareOfSum(5) == 225
test "square of sum 100":
check squareOfSum(100) == 25_502_500
test "sum of squares 1":
check sumOfSquares(1) == 1
test "sum of squares 5":
check sumOfSquares(5) == 55
test "sum of squares 100":
check sumOfSquares(100) == 338_350
test "difference of squares 1":
check difference(1) == 0
test "difference of squares 5":
check difference(5) == 170
test "difference of squares 100":
check difference(100) == 25_164_150
```

```
import math
# This returns the sum
#
# 1 + 2 + ... + n
#
# It is said that when he was a child, Gauss was given the problem of adding the
# first 100 numbers. He did it in no time, because he knew adding symmetric
# terms gave the same result:
#
# 1 + 100 = 101
# 2 + 99 = 101
# 3 + 98 = 101
# ...
# 50 + 51 = 101
#
# Then, you only need to think for a moment what happens when n is odd. Turns
# out, it does not matter, the formula is the same.
func sum(n: int): int =
n*(1 + n) div 2
func squareOfSum*(n: int): int =
sum(n)^2
# This is a known formula, but I do not recall an intuitive way to look at it.
func sumOfSquares*(n: int): int =
n*(n + 1)*(2*n + 1) div 6
func difference*(n: int): int =
squareOfSum(n) - sumOfSquares(n)
```

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?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments