Exercism v3 launches on Sept 1st 2021. Learn more! ðŸš€ðŸš€ðŸš€

Published at Mar 15 2021
·
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.

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3`

is used as it is the official build and
dependency management tool for erlang now. Please refer to the tracks installation
instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

```
$ rebar3 eunit
```

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

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.

```
%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/difference-of-squares/canonical-data.json
%% This file is automatically generated from the exercises canonical data.
-module(difference_of_squares_tests).
-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").
'1_square_of_sum_1_test_'() ->
{"square of sum 1",
?_assertEqual(1,
difference_of_squares:square_of_sum(1))}.
'2_square_of_sum_5_test_'() ->
{"square of sum 5",
?_assertEqual(225,
difference_of_squares:square_of_sum(5))}.
'3_square_of_sum_100_test_'() ->
{"square of sum 100",
?_assertEqual(25502500,
difference_of_squares:square_of_sum(100))}.
'4_sum_of_squares_1_test_'() ->
{"sum of squares 1",
?_assertEqual(1,
difference_of_squares:sum_of_squares(1))}.
'5_sum_of_squares_5_test_'() ->
{"sum of squares 5",
?_assertEqual(55,
difference_of_squares:sum_of_squares(5))}.
'6_sum_of_squares_100_test_'() ->
{"sum of squares 100",
?_assertEqual(338350,
difference_of_squares:sum_of_squares(100))}.
'7_difference_of_squares_1_test_'() ->
{"difference of squares 1",
?_assertEqual(0,
difference_of_squares:difference_of_squares(1))}.
'8_difference_of_squares_5_test_'() ->
{"difference of squares 5",
?_assertEqual(170,
difference_of_squares:difference_of_squares(5))}.
'9_difference_of_squares_100_test_'() ->
{"difference of squares 100",
?_assertEqual(25164150,
difference_of_squares:difference_of_squares(100))}.
```

```
-module(difference_of_squares).
-export([difference_of_squares/1, square_of_sum/1, sum_of_squares/1]).
difference_of_squares(_Number) -> square_of_sum(_Number) - sum_of_squares(_Number).
square_of_sum(_Number) -> Sum = _Number*(_Number + 1) div 2,
Sum*Sum.
sum_of_squares(_Number) -> _Number*(_Number + 1)*(2*_Number + 1) div 6.
```

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