diff --git a/elixir/difference-of-squares/.exercism/config.json b/elixir/difference-of-squares/.exercism/config.json new file mode 100644 index 0000000..4e90e69 --- /dev/null +++ b/elixir/difference-of-squares/.exercism/config.json @@ -0,0 +1,36 @@ +{ + "authors": [ + "petehuang" + ], + "contributors": [ + "andrewsardone", + "angelikatyborska", + "Cohen-Carlisle", + "dalexj", + "devonestes", + "jinyeow", + "kytrinyx", + "lpil", + "neenjaw", + "parkerl", + "rubysolo", + "Scientifica96", + "sotojuan", + "Teapane", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/squares.ex" + ], + "test": [ + "test/squares_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.", + "source": "Problem 6 at Project Euler", + "source_url": "https://projecteuler.net/problem=6" +} diff --git a/elixir/difference-of-squares/.exercism/metadata.json b/elixir/difference-of-squares/.exercism/metadata.json new file mode 100644 index 0000000..4493d69 --- /dev/null +++ b/elixir/difference-of-squares/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"difference-of-squares","id":"71804265a0684ed3ba15a88ed184ef81","url":"https://exercism.org/tracks/elixir/exercises/difference-of-squares","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/difference-of-squares/.formatter.exs b/elixir/difference-of-squares/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/difference-of-squares/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/difference-of-squares/.gitignore b/elixir/difference-of-squares/.gitignore new file mode 100644 index 0000000..bdedb1f --- /dev/null +++ b/elixir/difference-of-squares/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +difference_of_squares-*.tar + diff --git a/elixir/difference-of-squares/HELP.md b/elixir/difference-of-squares/HELP.md new file mode 100644 index 0000000..eeccae3 --- /dev/null +++ b/elixir/difference-of-squares/HELP.md @@ -0,0 +1,75 @@ +# Help + +## Running the tests + +From the terminal, change to the base directory of the exercise then execute the tests with: + +```bash +$ mix test +``` + +This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs` + +Documentation: + +* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html) +* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html) + +## Pending tests + +In test suites of practice exercises, all but the first test have been tagged to be 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: + +```elixir +# @tag :pending +test "shouting" do + assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" +end +``` + +If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command: + +```bash +$ mix test --include pending +``` + +Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`. + +```elixir +# ExUnit.configure(exclude: :pending, trace: true) +``` + +## Useful `mix test` options + +* `test/.exs:LINENUM` - runs only a single test, the test from `.exs` whose definition is on line `LINENUM` +* `--failed` - runs only tests that failed the last time they ran +* `--max-failures` - the suite stops evaluating tests when this number of test failures +is reached +* `--seed 0` - disables randomization so the tests in a single file will always be ran +in the same order they were defined in + +## Submitting your solution + +You can submit your solution using the `exercism submit lib/squares.ex` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir) +- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found. \ No newline at end of file diff --git a/elixir/difference-of-squares/README.md b/elixir/difference-of-squares/README.md new file mode 100644 index 0000000..88264e0 --- /dev/null +++ b/elixir/difference-of-squares/README.md @@ -0,0 +1,47 @@ +# Difference of Squares + +Welcome to Difference of Squares on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +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. + +## Source + +### Created by + +- @petehuang + +### Contributed to by + +- @andrewsardone +- @angelikatyborska +- @Cohen-Carlisle +- @dalexj +- @devonestes +- @jinyeow +- @kytrinyx +- @lpil +- @neenjaw +- @parkerl +- @rubysolo +- @Scientifica96 +- @sotojuan +- @Teapane +- @waiting-for-dev + +### Based on + +Problem 6 at Project Euler - https://projecteuler.net/problem=6 \ No newline at end of file diff --git a/elixir/difference-of-squares/lib/squares.ex b/elixir/difference-of-squares/lib/squares.ex new file mode 100644 index 0000000..9c6010d --- /dev/null +++ b/elixir/difference-of-squares/lib/squares.ex @@ -0,0 +1,29 @@ +defmodule Squares do + @moduledoc """ + Calculate sum of squares, square of sum, 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 + div(number * (number + 1) * (2 * number + 1), 6) + 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 + div(number * (number + 1), 2) ** 2 + 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 + abs(square_of_sum(number) - sum_of_squares(number)) + end +end diff --git a/elixir/difference-of-squares/mix.exs b/elixir/difference-of-squares/mix.exs new file mode 100644 index 0000000..1d6f867 --- /dev/null +++ b/elixir/difference-of-squares/mix.exs @@ -0,0 +1,29 @@ +defmodule Squares.MixProject do + use Mix.Project + + def project do + [ + app: :squares, + version: "0.1.0", + # elixir: "~> 1.8", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + {:lettuce, "~> 0.3.0", only: ~w(dev)a} + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/elixir/difference-of-squares/mix.lock b/elixir/difference-of-squares/mix.lock new file mode 100644 index 0000000..2fa5dec --- /dev/null +++ b/elixir/difference-of-squares/mix.lock @@ -0,0 +1,3 @@ +%{ + "lettuce": {:hex, :lettuce, "0.3.0", "823198f053714282f980acc68c7157b9c78c740910cb4f572a642e020417a850", [:mix], [], "hexpm", "a47479d94ac37460481133213f08c8283dabbe762f4f8f8028456500d1fca9c4"}, +} diff --git a/elixir/difference-of-squares/test/squares_test.exs b/elixir/difference-of-squares/test/squares_test.exs new file mode 100644 index 0000000..6f7af4c --- /dev/null +++ b/elixir/difference-of-squares/test/squares_test.exs @@ -0,0 +1,45 @@ +defmodule SquaresTest do + use ExUnit.Case + + describe "square_of_sum" do + test "square of sum to 1" do + assert Squares.square_of_sum(1) == 1 + end + + test "square of sum to 5" do + assert Squares.square_of_sum(5) == 225 + end + + test "square of sum to 100" do + assert Squares.square_of_sum(100) == 25_502_500 + end + end + + describe "sum_of_squares" do + test "sum of squares to 1" do + assert Squares.sum_of_squares(1) == 1 + end + + test "sum of squares to 5" do + assert Squares.sum_of_squares(5) == 55 + end + + test "sum of squares to 100" do + assert Squares.sum_of_squares(100) == 338_350 + end + end + + describe "difference" do + test "difference of sum to 1" do + assert Squares.difference(1) == 0 + end + + test "difference of sum to 5" do + assert Squares.difference(5) == 170 + end + + test "difference of sum to 100" do + assert Squares.difference(100) == 25_164_150 + end + end +end diff --git a/elixir/difference-of-squares/test/test_helper.exs b/elixir/difference-of-squares/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/difference-of-squares/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)