Sha256: 2b142c5229c70c1045075b3b92c6a796bf50aaba05c0fd5aecc30ddded41e320

Contents?: true

Size: 887 Bytes

Versions: 69

Compression:

Stored size: 887 Bytes

Contents

defmodule Squares do
  @moduledoc """
  Calculate sum of squares, square of sums, difference between two sums from 1 to a given end number.
  """

  @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
    Enum.reduce(1..number, 0, fn x, acc -> acc + x * x end)
  end

  @doc """
  Calculate square of sums from 1 to a given end number.
  """
  @spec square_of_sums(pos_integer) :: pos_integer
  def square_of_sums(number) do
    sum_to_number = Enum.reduce(1..number, fn x, acc -> x + acc end)
    sum_to_number * sum_to_number
  end

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

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/difference-of-squares/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/difference-of-squares/example.exs