🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of juliagoda

juliagoda's solution

to Nth Prime in the Haskell Track

Published at Oct 28 2019 · 0 comments
Test suite

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.

Getting Started

Please refer to the installation and learning help pages.

Running the tests

To run the test suite, execute the following command:

stack test

If you get an error message like this...

No .cabal file found in directory

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

Otherwise, if you get an error message like this...

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

Running GHCi

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

stack ghci

Feedback, Issues, Pull Requests

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

Submitting Incomplete Solutions

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

    test Case{..} = it description assertion
        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 [] = []

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?