This commit is contained in:
Danil Negrienko 2024-06-27 01:56:32 -04:00
parent eb76c60826
commit fcc4ddb61c
10 changed files with 273 additions and 0 deletions

View File

@ -0,0 +1,34 @@
{
"authors": [
"petehuang"
],
"contributors": [
"andrewsardone",
"angelikatyborska",
"Cohen-Carlisle",
"dalexj",
"devonestes",
"jinyeow",
"lpil",
"neenjaw",
"parkerl",
"rubysolo",
"sotojuan",
"Teapane",
"waiting-for-dev"
],
"files": {
"solution": [
"lib/prime.ex"
],
"test": [
"test/prime_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Given a number n, determine what the nth prime is.",
"source": "A variation on Problem 7 at Project Euler",
"source_url": "https://projecteuler.net/problem=7"
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"nth-prime","id":"78fc419075f2478d95bd6d815faa8882","url":"https://exercism.org/tracks/elixir/exercises/nth-prime","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/nth-prime/.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").
nth_prime-*.tar

75
elixir/nth-prime/HELP.md Normal file
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.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,42 @@
# Nth Prime
Welcome to Nth Prime on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Given a number n, determine what the nth prime is.
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.
## 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
- @petehuang
### Contributed to by
- @andrewsardone
- @angelikatyborska
- @Cohen-Carlisle
- @dalexj
- @devonestes
- @jinyeow
- @lpil
- @neenjaw
- @parkerl
- @rubysolo
- @sotojuan
- @Teapane
- @waiting-for-dev
### Based on
A variation on Problem 7 at Project Euler - https://projecteuler.net/problem=7

View File

@ -0,0 +1,30 @@
defmodule Prime do
@doc """
Generates the nth prime.
"""
@spec nth(non_neg_integer) :: non_neg_integer
def nth(count) when count >= 1 do
0
|> Stream.iterate(&(&1 + 1))
|> Stream.filter(&prime?/1)
|> Stream.take(count + 1)
|> Enum.to_list()
|> List.last()
end
@spec prime?(non_neg_integer) :: boolean
defp prime?(number) when number in 1..3, do: true
defp prime?(number) when rem(number, 2) == 0, do: false
defp prime?(number) when number > 3, do: check_prime(number)
defp check_prime(number, prime \\ 3)
defp check_prime(number, number), do: true
defp check_prime(number, prime) do
if prime >= trunc(number ** 0.5) + 1,
do: true,
else: rem(number, prime) != 0 && check_prime(number, prime + 2)
end
# defp check_prime(number, prime), do: rem(number, prime) != 0 && check_prime(number, prime + 2)
end

28
elixir/nth-prime/mix.exs Normal file
View File

@ -0,0 +1,28 @@
defmodule Prime.MixProject do
use Mix.Project
def project do
[
app: :prime,
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,33 @@
defmodule PrimeTest do
use ExUnit.Case
test "first prime" do
assert Prime.nth(1) == 2
end
test "second prime" do
assert Prime.nth(2) == 3
end
test "sixth prime" do
assert Prime.nth(6) == 13
end
test "100th prime" do
assert Prime.nth(100) == 541
end
@tag :slow
test "big prime" do
assert Prime.nth(10001) == 104_743
end
@tag :slow
test "very big prime" do
assert Prime.nth(100001) == 1_299_721
end
test "there is no zeroth prime" do
catch_error(Prime.nth(0))
end
end

View File

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