🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # angelikatyborska's solution

## to Difference Of Squares in the Erlang Track

Published at Mar 30 2019 · 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.

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.

## Running tests

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

## Questions?

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.

## 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_tests.erl

``````%% Based on canonical data version 1.2.0
%% https://github.com/exercism/problem-specifications/raw/master/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'() ->
?assertEqual(1, difference_of_squares:square_of_sum(1)).

'2_square_of_sum_5_test'() ->
?assertEqual(225,
difference_of_squares:square_of_sum(5)).

'3_square_of_sum_100_test'() ->
?assertEqual(25502500,
difference_of_squares:square_of_sum(100)).

'4_sum_of_squares_1_test'() ->
?assertEqual(1,
difference_of_squares:sum_of_squares(1)).

'5_sum_of_squares_5_test'() ->
?assertEqual(55,
difference_of_squares:sum_of_squares(5)).

'6_sum_of_squares_100_test'() ->
?assertEqual(338350,
difference_of_squares:sum_of_squares(100)).

'7_difference_of_squares_1_test'() ->
?assertEqual(0,
difference_of_squares:difference_of_squares(1)).

'8_difference_of_squares_5_test'() ->
?assertEqual(170,
difference_of_squares:difference_of_squares(5)).

'9_difference_of_squares_100_test'() ->
?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) ->
Range = lists:seq(1, Number),
Sum = lists:sum(Range),
Sum * Sum.

sum_of_squares(Number) ->
Range = lists:seq(1, Number),
Squares = lists:map(fun(N) -> N * N end, Range),
lists:sum(Squares).``````

## 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?