Published at Dec 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.

For installation and learning resources, refer to the exercism help page.

To work on the exercises, you will need `Opam`

and `Base`

. Consult opam website for instructions on how to install `opam`

for your OS. Once `opam`

is installed open a terminal window and run the following command to install base:

```
opam install base
```

To run the tests you will need `OUnit`

. Install it using `opam`

:

```
opam install ounit
```

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

```
make
```

`utop`

is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

```
opam install utop
```

Consult utop for more detail.

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to 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.

```
open OUnit2
open Difference_of_squares
let ae exp got _test_ctxt = assert_equal exp got
let square_of_sum_tests = [
"square of sum 1" >::
ae 1 (square_of_sum 1);
"square of sum 5" >::
ae 225 (square_of_sum 5);
"square of sum 100" >::
ae 25502500 (square_of_sum 100);
]
let sum_of_squares_tests = [
"sum of squares 1" >::
ae 1 (sum_of_squares 1);
"sum of squares 5" >::
ae 55 (sum_of_squares 5);
"sum of squares 100" >::
ae 338350 (sum_of_squares 100);
]
let difference_of_squares_tests = [
"difference of squares 1" >::
ae 0 (difference_of_squares 1);
"difference of squares 5" >::
ae 170 (difference_of_squares 5);
"difference of squares 100" >::
ae 25164150 (difference_of_squares 100);
]
let () =
run_test_tt_main (
"difference of squares tests" >:::
List.concat [square_of_sum_tests; sum_of_squares_tests; difference_of_squares_tests]
)
```

```
open Base
let errstr = "n must be non-negative"
let square n = n * n
let rec do_sum n a fstep =
match n with
| 0 -> a
| n -> do_sum (n - 1) (a + (fstep n)) fstep
let square_of_sum (n : int) =
if n < 0 then raise (Invalid_argument errstr)
else do_sum n 0 (fun n -> n) |> square
let sum_of_squares (n : int) =
if n < 0 then raise (Invalid_argument errstr)
else do_sum n 0 square
let difference_of_squares (n : int) =
if n < 0 then raise (Invalid_argument errstr)
else (square_of_sum n) - (sum_of_squares 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,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments