@@ -0,0 +1,15 @@ | |||
module NthPrime | |||
let isPrime n = | |||
let stop = int(sqrt(float(n))) | |||
seq { 2..stop } | |||
|> Seq.exists (fun x -> n % x = 0) | |||
|> not | |||
let oddNumbers = Seq.initInfinite (fun i -> i + 2) | |||
let oddPrimes = Seq.filter isPrime oddNumbers | |||
let safeNthPrime n = if n = 1 then 2 else Seq.last (Seq.take n oddPrimes) | |||
let prime nth : int option = if nth = 0 then None else Some(safeNthPrime nth) |
@@ -0,0 +1,21 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<TargetFramework>netcoreapp3.0</TargetFramework> | |||
<IsPackable>false</IsPackable> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Compile Include="NthPrime.fs" /> | |||
<Compile Include="NthPrimeTests.fs" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> | |||
<PackageReference Include="xunit" Version="2.4.1" /> | |||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" /> | |||
<PackageReference Include="FsUnit.xUnit" Version="3.9.0" /> | |||
</ItemGroup> | |||
</Project> |
@@ -0,0 +1,29 @@ | |||
// This file was auto-generated based on version 2.1.0 of the canonical data. | |||
module NthPrimeTests | |||
open FsUnit.Xunit | |||
open Xunit | |||
open NthPrime | |||
[<Fact>] | |||
let ``First prime`` () = | |||
prime 1 |> should equal (Some 2) | |||
[<Fact>] | |||
let ``Second prime`` () = | |||
prime 2 |> should equal (Some 3) | |||
[<Fact>] | |||
let ``Sixth prime`` () = | |||
prime 6 |> should equal (Some 13) | |||
[<Fact>] | |||
let ``Big prime`` () = | |||
prime 10001 |> should equal (Some 104743) | |||
[<Fact>] | |||
let ``There is no zeroth prime`` () = | |||
prime 0 |> should equal None | |||
@@ -0,0 +1,35 @@ | |||
# Nth Prime | |||
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. | |||
## Hints | |||
For this exercise the following F# feature comes in handy: | |||
- [Sequences](https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/sequences) are evaluated lazily. They allows you to work with an infinite sequence of values. | |||
Note: to help speedup calculation, you should not check numbers which you know beforehand will never be prime. For more information, see the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes). | |||
## Running the tests | |||
To run the tests, run the command `dotnet test` from within the exercise directory. | |||
## Autoformatting the code | |||
F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool. | |||
After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory. | |||
## Further information | |||
For more detailed information about the F# track, including how to get help if | |||
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources). | |||
## Source | |||
A variation on Problem 7 at Project Euler [http://projecteuler.net/problem=7](http://projecteuler.net/problem=7) | |||