Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of isroman

isroman's solution

to Difference Of Squares in the Erlang Track

Published at Apr 20 2021 · 0 comments
Test suite

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


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

Submitting Incomplete Solutions

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.



'1_square_of_sum_1_test_'() ->
    {"square of sum 1",

'2_square_of_sum_5_test_'() ->
    {"square of sum 5",

'3_square_of_sum_100_test_'() ->
    {"square of sum 100",

'4_sum_of_squares_1_test_'() ->
    {"sum of squares 1",

'5_sum_of_squares_5_test_'() ->
    {"sum of squares 5",

'6_sum_of_squares_100_test_'() ->
    {"sum of squares 100",

'7_difference_of_squares_1_test_'() ->
    {"difference of squares 1",

'8_difference_of_squares_5_test_'() ->
    {"difference of squares 5",

'9_difference_of_squares_100_test_'() ->
    {"difference of squares 100",

-export([difference_of_squares/1, square_of_sum/1, sum_of_squares/1]).

difference_of_squares(Number) ->
  abs(square_of_sum(Number) - sum_of_squares(Number)).

square_of_sum(Number) ->
  Sum = (Number * (1 + Number)) / 2,
  ceil(Sum * Sum).

sum_of_squares(Number) ->
  sum_of_squares(Number, 0).

sum_of_squares(0, Sum) ->
sum_of_squares(Number, Sum) ->
  sum_of_squares(Number - 1, Sum + Number * Number).

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?