 # davearonson's solution

## to Nth Prime in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

## Running tests

Execute the tests with:

``````\$ elixir nth_prime_test.exs
``````

### Pending tests

In the test suites, all but the first test have been 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:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

For more detailed information about the Elixir track, please see the help page.

## Source

A variation on Problem 7 at Project Euler http://projecteuler.net/problem=7

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### nth_prime_test.exs

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule NthPrimeTest do
use ExUnit.Case

# @tag :pending
test "first prime" do
assert Prime.nth(1) == 2
end

@tag :pending
test "second prime" do
assert Prime.nth(2) == 3
end

@tag :pending
test "sixth prime" do
assert Prime.nth(6) == 13
end

@tag :pending
test "100th prime" do
assert Prime.nth(100) == 541
end

@tag :pending
test "weird case" do
catch_error(Prime.nth(0))
end
end``````
``````defmodule Prime do

@doc """
Generates the nth prime.
"""
@spec nth(non_neg_integer) :: non_neg_integer
# we COULD handle 0 with this implementation, but test says raise error
def nth(count) when count <= 0, do: raise ArgumentError
def nth(count) do
List.last(primes_list(count, [], 2))
end

#
# - Could have not bothered caching the primes we've found so far, and just
# counted them... but then we'd have to check ALL numbers up to the limit to
# see if the current candidate is a multiple, not just the primes, which are
# a small subset.
#
# - Could have PREpended to the "primes so far" list, and taken the FIRST one
# instead of the last in nth, and reversed before the take_while... but even
# though appending and taking the last are far less efficient, the former
# happens only on primes and the latter happens only once, but they let us
# skip the reversal at *every* step.
#
# - Could have gotten the numbers as a Stream.  That was in fact what I first
# tried.  But it made other things more complex, assuming I wanted to still
# cache the "primes so far" list.

def primes_list(how_many, primes_so_far, candidate) do
cond do
Enum.count(primes_so_far) == how_many ->
primes_so_far
any_factors(primes_so_far, candidate) ->
primes_list(how_many, primes_so_far, candidate + 1)
true ->
primes_list(how_many, primes_so_far ++ [candidate], candidate + 1)
end
end

defp any_factors(primes_so_far, candidate) do
primes_so_far
|> Enum.take_while(&(&1 <= :math.sqrt(candidate)))
|> Enum.any?(&(is_multiple?(&1, candidate)))
end

defp is_multiple?(factor, multiple) do
rem(multiple, factor) == 0
end

end``````