ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Oct 28 2019
·
0 comments

Instructions

Test suite

Solution

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.

Please refer to the installation and learning help pages.

To run the test suite, execute the following command:

```
stack test
```

```
No .cabal file found in directory
```

You are probably running an old stack version and need to upgrade it.

```
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
```

Just do as it says and it will download and install the correct compiler version:

```
stack setup
```

If you want to play with your solution in GHCi, just run the command:

```
stack ghci
```

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

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

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

```
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE RecordWildCards #-}
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import Prime (nth)
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs
specs :: Spec
specs = describe "nth" $ for_ cases test
where
test Case{..} = it description assertion
where
assertion = nth (fromIntegral input) `shouldBe` expected
data Case = Case { description :: String
, input :: Integer
, expected :: Maybe Integer
}
cases :: [Case]
cases = [ Case { description = "first prime"
, input = 1
, expected = Just 2
}
, Case { description = "second prime"
, input = 2
, expected = Just 3
}
, Case { description = "sixth prime"
, input = 6
, expected = Just 13
}
, Case { description = "big prime"
, input = 10001
, expected = Just 104743
}
, Case { description = "there is no zeroth prime"
, input = 0
, expected = Nothing
}
]
```

```
module Prime (nth) where
nth :: Integral a => Int -> Maybe a
nth 0 = Nothing
nth x = Just (last $ primes x)
primes :: Integral a => Int -> [a]
primes n = take n $ sieve [2..]
where sieve (p:xs) = p : sieve [x | x <- xs, x `rem` p > 0]
sieve [] = []
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,429 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments