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

oyms's solution

to Nth Prime in the Haskell Track

Published at Oct 15 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.

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!

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.

Tests.hs

{-# 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

import Data.Maybe

nth :: Int -> Maybe Integer
nth 1 = Just 2
nth 2 = Just 3
nth n 
    | n < 1 = Nothing
    | otherwise = Just (nextPrime (fromJust (nth (n - 1))))

nextPrime :: Integer -> Integer
nextPrime previous
    | mod previous 2 == 0 = nextPrime previous + 1
    | isPrime (previous + 2) = previous + 2
    | otherwise = nextPrime (previous + 2)

isPrime :: Integer -> Bool
isPrime n
    | n <= 3 = n > 1
    | divisibleBy 2 = False
    | divisibleBy 3 = False
    | n < 25 = True
    | any 
        (\k -> divisibleBy k || divisibleBy (k + 2)) 
        [5,11..floor(sqrt $ fromIntegral n)] 
            = False
    | otherwise = True
    where
        divisibleBy x = mod n x == 0

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?