prime-factors

This commit is contained in:
Danil Negrienko 2024-08-20 22:15:20 -04:00
parent 00a1a882dd
commit 4e5b4dceeb
10 changed files with 320 additions and 0 deletions

View File

@ -0,0 +1,33 @@
{
"authors": [
"rubysolo"
],
"contributors": [
"andrewsardone",
"angelikatyborska",
"Cohen-Carlisle",
"dalexj",
"devonestes",
"jinyeow",
"lpil",
"neenjaw",
"parkerl",
"sotojuan",
"Teapane",
"waiting-for-dev"
],
"files": {
"solution": [
"lib/prime_factors.ex"
],
"test": [
"test/prime_factors_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Compute the prime factors of a given natural number.",
"source": "The Prime Factors Kata by Uncle Bob",
"source_url": "https://web.archive.org/web/20221026171801/http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata"
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"prime-factors","id":"d60336d59e4e428da1c88cc5a540f017","url":"https://exercism.org/tracks/elixir/exercises/prime-factors","handle":"negrienko","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

24
elixir/prime-factors/.gitignore vendored Normal file
View File

@ -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").
prime_factors-*.tar

View File

@ -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/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.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/prime_factors.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.

View File

@ -0,0 +1,70 @@
# Prime Factors
Welcome to Prime Factors on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Compute the prime factors of a given natural number.
A prime number is only evenly divisible by itself and 1.
Note that 1 is not a prime number.
## Example
What are the prime factors of 60?
- Our first divisor is 2.
2 goes into 60, leaving 30.
- 2 goes into 30, leaving 15.
- 2 doesn't go cleanly into 15.
So let's move on to our next divisor, 3.
- 3 goes cleanly into 15, leaving 5.
- 3 does not go cleanly into 5.
The next possible factor is 4.
- 4 does not go cleanly into 5.
The next possible factor is 5.
- 5 does go cleanly into 5.
- We're left only with 1, so now, we're done.
Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.
You can check this yourself:
```text
2 * 2 * 3 * 5
= 4 * 15
= 60
```
Success!
## Slow tests
One or several of the tests of this exercise have been tagged as `:slow`, because they might take a long time to finish. For this reason, they will not be run on the platform by the automated test runner. If you are solving this exercise directly on the platform in the web editor, you might want to consider downloading this exercise to your machine instead. This will allow you to run all the tests and check the efficiency of your solution.
## Source
### Created by
- @rubysolo
### Contributed to by
- @andrewsardone
- @angelikatyborska
- @Cohen-Carlisle
- @dalexj
- @devonestes
- @jinyeow
- @lpil
- @neenjaw
- @parkerl
- @sotojuan
- @Teapane
- @waiting-for-dev
### Based on
The Prime Factors Kata by Uncle Bob - https://web.archive.org/web/20221026171801/http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

View File

@ -0,0 +1,22 @@
defmodule PrimeFactors do
@doc """
Compute the prime factors for 'number'.
The prime factors are prime numbers that when multiplied give the desired
number.
The prime factors of 'number' will be ordered lowest to highest.
"""
@spec factors_for(pos_integer) :: [pos_integer]
def factors_for(number), do: do_factors_for(number)
defp do_factors_for(number, acc \\ [], divisor \\ 2)
defp do_factors_for(1, acc, _divisor), do: Enum.reverse(acc)
defp do_factors_for(number, acc, divisor) when rem(number, divisor) == 0 do
case divisor < trunc(number ** 0.5) + 1 do
true -> do_factors_for(div(number, divisor), [divisor | acc])
false -> Enum.reverse([divisor | acc])
end
end
defp do_factors_for(number, acc, divisor), do: do_factors_for(number, acc, divisor + 1)
end

View File

@ -0,0 +1,28 @@
defmodule PrimeFactors.MixProject do
use Mix.Project
def project do
[
app: :prime_factors,
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

View File

@ -0,0 +1,61 @@
defmodule PrimeFactorsTest do
use ExUnit.Case
test "1" do
assert PrimeFactors.factors_for(1) == []
end
test "2" do
assert PrimeFactors.factors_for(2) == [2]
end
test "3" do
assert PrimeFactors.factors_for(3) == [3]
end
test "4" do
assert PrimeFactors.factors_for(4) == [2, 2]
end
test "6" do
assert PrimeFactors.factors_for(6) == [2, 3]
end
test "8" do
assert PrimeFactors.factors_for(8) == [2, 2, 2]
end
test "9" do
assert PrimeFactors.factors_for(9) == [3, 3]
end
test "12" do
assert PrimeFactors.factors_for(12) == [2, 2, 3]
end
test "27" do
assert PrimeFactors.factors_for(27) == [3, 3, 3]
end
test "625" do
assert PrimeFactors.factors_for(625) == [5, 5, 5, 5]
end
test "901255" do
assert PrimeFactors.factors_for(901_255) == [5, 17, 23, 461]
end
test "93819012551" do
assert PrimeFactors.factors_for(93_819_012_551) == [11, 9539, 894_119]
end
@tag :slow
# @tag timeout: 2000
#
# The timeout tag above will set the below test to fail unless it completes
# in under two seconds. Uncomment it if you want to test the efficiency of your
# solution.
test "10000000055" do
assert PrimeFactors.factors_for(10_000_000_055) == [5, 2_000_000_011]
end
end

View File

@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)