Avatar of paulfioravanti

paulfioravanti's solution

to Difference Of Squares in the Elixir Track

Published at Jun 18 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.

Running tests

Execute the tests with:

$ elixir difference_of_squares_test.exs

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("difference_of_squares.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule DifferenceOfSquaresTest do
  use ExUnit.Case

  # @tag :pending
  test "square of sum to 5" do
    assert Squares.square_of_sum(5) == 225
  end

  @tag :pending
  test "sum of squares to 5" do
    assert Squares.sum_of_squares(5) == 55
  end

  @tag :pending
  test "difference of sum to 5" do
    assert Squares.difference(5) == 170
  end

  @tag :pending
  test "square of sum to 10" do
    assert Squares.square_of_sum(10) == 3025
  end

  @tag :pending
  test "sum of squares to 10" do
    assert Squares.sum_of_squares(10) == 385
  end

  @tag :pending
  test "difference of sum to 10" do
    assert Squares.difference(10) == 2640
  end

  @tag :pending
  test "square of sum to 100" do
    assert Squares.square_of_sum(100) == 25_502_500
  end

  @tag :pending
  test "sum of squares to 100" do
    assert Squares.sum_of_squares(100) == 338_350
  end

  @tag :pending
  test "difference of sum to 100" do
    assert Squares.difference(100) == 25_164_150
  end
end
defmodule Squares do
  @moduledoc """
  Calculate sum of squares, square of sum, difference between two sums from 1 to a given end number.
  """

  @square 2

  @doc """
  Calculate sum of squares from 1 to a given end number.
  """
  @spec sum_of_squares(pos_integer) :: pos_integer
  def sum_of_squares(number) do
    1..number
    |> Enum.reduce(&add_square/2)
  end

  @doc """
  Calculate square of sum from 1 to a given end number.
  """
  @spec square_of_sum(pos_integer) :: pos_integer
  def square_of_sum(number) do
    1..number
    |> Enum.sum()
    |> :math.pow(@square)
  end

  @doc """
  Calculate difference between sum of squares and square of sum from 1 to a given end number.
  """
  @spec difference(pos_integer) :: pos_integer
  def difference(number) do
    square_of_sum(number) - sum_of_squares(number)
  end

  defp add_square(number, acc) do
    number
    |> :math.pow(@square)
    |> Kernel.+(acc)
  end
end

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?