From 00a1a882dd378b4d8b6a930c0c8436b9ee964db0 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Sun, 7 Jul 2024 20:55:46 -0400 Subject: [PATCH] perfect-numbers --- elixir/perfect-numbers/.exercism/config.json | 26 +++++++ .../perfect-numbers/.exercism/metadata.json | 1 + elixir/perfect-numbers/.formatter.exs | 4 + elixir/perfect-numbers/.gitignore | 24 ++++++ elixir/perfect-numbers/HELP.md | 75 +++++++++++++++++++ elixir/perfect-numbers/README.md | 62 +++++++++++++++ elixir/perfect-numbers/lib/perfect_numbers.ex | 41 ++++++++++ elixir/perfect-numbers/mix.exs | 28 +++++++ .../test/perfect_numbers_test.exs | 65 ++++++++++++++++ elixir/perfect-numbers/test/test_helper.exs | 2 + 10 files changed, 328 insertions(+) create mode 100644 elixir/perfect-numbers/.exercism/config.json create mode 100644 elixir/perfect-numbers/.exercism/metadata.json create mode 100644 elixir/perfect-numbers/.formatter.exs create mode 100644 elixir/perfect-numbers/.gitignore create mode 100644 elixir/perfect-numbers/HELP.md create mode 100644 elixir/perfect-numbers/README.md create mode 100644 elixir/perfect-numbers/lib/perfect_numbers.ex create mode 100644 elixir/perfect-numbers/mix.exs create mode 100644 elixir/perfect-numbers/test/perfect_numbers_test.exs create mode 100644 elixir/perfect-numbers/test/test_helper.exs diff --git a/elixir/perfect-numbers/.exercism/config.json b/elixir/perfect-numbers/.exercism/config.json new file mode 100644 index 0000000..663ab76 --- /dev/null +++ b/elixir/perfect-numbers/.exercism/config.json @@ -0,0 +1,26 @@ +{ + "authors": [ + "DoggettCK" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "neenjaw", + "sotojuan" + ], + "files": { + "solution": [ + "lib/perfect_numbers.ex" + ], + "test": [ + "test/perfect_numbers_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.", + "source": "Taken from Chapter 2 of Functional Thinking by Neal Ford.", + "source_url": "https://www.oreilly.com/library/view/functional-thinking/9781449365509/" +} diff --git a/elixir/perfect-numbers/.exercism/metadata.json b/elixir/perfect-numbers/.exercism/metadata.json new file mode 100644 index 0000000..12914dd --- /dev/null +++ b/elixir/perfect-numbers/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"perfect-numbers","id":"a3a905652d3c4b7fbe13a8ea5819052b","url":"https://exercism.org/tracks/elixir/exercises/perfect-numbers","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/perfect-numbers/.formatter.exs b/elixir/perfect-numbers/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/perfect-numbers/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/perfect-numbers/.gitignore b/elixir/perfect-numbers/.gitignore new file mode 100644 index 0000000..28b7818 --- /dev/null +++ b/elixir/perfect-numbers/.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"). +perfect_numbers-*.tar + diff --git a/elixir/perfect-numbers/HELP.md b/elixir/perfect-numbers/HELP.md new file mode 100644 index 0000000..817684a --- /dev/null +++ b/elixir/perfect-numbers/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/perfect_numbers.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/perfect-numbers/README.md b/elixir/perfect-numbers/README.md new file mode 100644 index 0000000..1f4c614 --- /dev/null +++ b/elixir/perfect-numbers/README.md @@ -0,0 +1,62 @@ +# Perfect Numbers + +Welcome to Perfect Numbers on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers. + +The Greek mathematician [Nicomachus][nicomachus] devised a classification scheme for positive integers, identifying each as belonging uniquely to the categories of [perfect](#perfect), [abundant](#abundant), or [deficient](#deficient) based on their [aliquot sum][aliquot-sum]. +The _aliquot sum_ is defined as the sum of the factors of a number not including the number itself. +For example, the aliquot sum of `15` is `1 + 3 + 5 = 9`. + +## Perfect + +A number is perfect when it equals its aliquot sum. +For example: + +- `6` is a perfect number because `1 + 2 + 3 = 6` +- `28` is a perfect number because `1 + 2 + 4 + 7 + 14 = 28` + +## Abundant + +A number is abundant when it is less than its aliquot sum. +For example: + +- `12` is an abundant number because `1 + 2 + 3 + 4 + 6 = 16` +- `24` is an abundant number because `1 + 2 + 3 + 4 + 6 + 8 + 12 = 36` + +## Deficient + +A number is deficient when it is greater than its aliquot sum. +For example: + +- `8` is a deficient number because `1 + 2 + 4 = 7` +- Prime numbers are deficient + +## Task + +Implement a way to determine whether a given number is [perfect](#perfect). +Depending on your language track, you may also need to implement a way to determine whether a given number is [abundant](#abundant) or [deficient](#deficient). + +[nicomachus]: https://en.wikipedia.org/wiki/Nicomachus +[aliquot-sum]: https://en.wikipedia.org/wiki/Aliquot_sum + +## Source + +### Created by + +- @DoggettCK + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @neenjaw +- @sotojuan + +### Based on + +Taken from Chapter 2 of Functional Thinking by Neal Ford. - https://www.oreilly.com/library/view/functional-thinking/9781449365509/ \ No newline at end of file diff --git a/elixir/perfect-numbers/lib/perfect_numbers.ex b/elixir/perfect-numbers/lib/perfect_numbers.ex new file mode 100644 index 0000000..3d64728 --- /dev/null +++ b/elixir/perfect-numbers/lib/perfect_numbers.ex @@ -0,0 +1,41 @@ +defmodule PerfectNumbers do + @doc """ + Determine the aliquot sum of the given `number`, by summing all the factors + of `number`, aside from `number` itself. + + Based on this sum, classify the number as: + + :perfect if the aliquot sum is equal to `number` + :abundant if the aliquot sum is greater than `number` + :deficient if the aliquot sum is less than `number` + """ + @spec classify(number :: integer) :: {:ok, atom} | {:error, String.t()} + def classify(number) when number <= 0, do: {:error, "Classification is only possible for natural numbers."} + def classify(1), do: {:ok, :deficient} + def classify(number) do + sum = + number + |> divisors() + |> Enum.sum() + + cond do + sum == number -> + {:ok, :perfect} + + sum > number -> + {:ok, :abundant} + + sum < number -> + {:ok, :deficient} + end + end + + defp divisors(number) do + for i <- 1..round(number ** 0.5), reduce: [] do + acc -> if rem(number, i) == 0, do: [i, div(number, i) | acc], else: acc + end + |> Enum.uniq() + |> Enum.sort() + |> Enum.drop(-1) + end +end diff --git a/elixir/perfect-numbers/mix.exs b/elixir/perfect-numbers/mix.exs new file mode 100644 index 0000000..25765b3 --- /dev/null +++ b/elixir/perfect-numbers/mix.exs @@ -0,0 +1,28 @@ +defmodule PerfectNumbers.MixProject do + use Mix.Project + + def project do + [ + app: :perfect_numbers, + 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 + [ + # {: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/perfect-numbers/test/perfect_numbers_test.exs b/elixir/perfect-numbers/test/perfect_numbers_test.exs new file mode 100644 index 0000000..06cf710 --- /dev/null +++ b/elixir/perfect-numbers/test/perfect_numbers_test.exs @@ -0,0 +1,65 @@ +defmodule PerfectNumbersTest do + use ExUnit.Case + + describe "Perfect numbers" do + test "Smallest perfect number is classified correctly" do + assert PerfectNumbers.classify(6) == {:ok, :perfect} + end + + test "Medium perfect number is classified correctly" do + assert PerfectNumbers.classify(28) == {:ok, :perfect} + end + + test "Large perfect number is classified correctly" do + assert PerfectNumbers.classify(33_550_336) == {:ok, :perfect} + end + end + + describe "Abundant numbers" do + test "Smallest abundant number is classified correctly" do + assert PerfectNumbers.classify(12) == {:ok, :abundant} + end + + test "Medium abundant number is classified correctly" do + assert PerfectNumbers.classify(30) == {:ok, :abundant} + end + + test "Large abundant number is classified correctly" do + assert PerfectNumbers.classify(33_550_335) == {:ok, :abundant} + end + end + + describe "Deficient numbers" do + test "Smallest prime deficient number is classified correctly" do + assert PerfectNumbers.classify(2) == {:ok, :deficient} + end + + test "Smallest non-prime deficient number is classified correctly" do + assert PerfectNumbers.classify(4) == {:ok, :deficient} + end + + test "Medium deficient number is classified correctly" do + assert PerfectNumbers.classify(32) == {:ok, :deficient} + end + + test "Large deficient number is classified correctly" do + assert PerfectNumbers.classify(33_550_337) == {:ok, :deficient} + end + + test "Edge case (no factors other than itself) is classified correctly" do + assert PerfectNumbers.classify(1) == {:ok, :deficient} + end + end + + describe "Invalid inputs" do + test "Zero is rejected (not a natural number)" do + assert PerfectNumbers.classify(0) == + {:error, "Classification is only possible for natural numbers."} + end + + test "Negative integer is rejected (not a natural number)" do + assert PerfectNumbers.classify(-1) == + {:error, "Classification is only possible for natural numbers."} + end + end +end diff --git a/elixir/perfect-numbers/test/test_helper.exs b/elixir/perfect-numbers/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/perfect-numbers/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)