Last active
March 7, 2018 10:39
-
-
Save mrtank/57c60cbb84b819d3e130fee455e89541 to your computer and use it in GitHub Desktop.
PrimeFactorsFsharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace PrimeFactors | |
module PrimeFactors = | |
type IPrimeChecker = | |
abstract member IsPrime : int64 -> int64 -> bool | |
type PrimeFactors() = | |
interface IPrimeChecker with | |
member this.IsPrime number i = | |
number % i = 0L | |
member this.CheckPrimes number i acc = | |
if i > (number |> float |> sqrt |> int64) then | |
number::acc | |
elif (this :> IPrimeChecker).IsPrime number i then | |
this.CheckPrimes (number / i) i (i::acc) | |
else | |
this.CheckPrimes number (i + 1L) acc | |
member this.Of number = | |
if number = 1L then | |
[||] | |
else | |
this.CheckPrimes number 2L [] |> List.toArray | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace PrimeFactors | |
open PrimeFactors | |
module PrimeFactorsMock = | |
type PrimeFactorsMock() = | |
inherit PrimeFactors() | |
let mutable stepCount = 0L | |
interface IPrimeChecker with | |
member this.IsPrime number i = | |
stepCount <- stepCount + 1L | |
number % i = 0L | |
member this.StepCount number = | |
stepCount <- 0L | |
this.Of number |> ignore | |
stepCount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace PrimeFactors | |
open PrimeFactors | |
open NUnit.Framework | |
open FsUnit | |
open PrimeFactorsMock | |
[<TestFixture>] | |
module PrimeFactorTests = | |
[<TestCase(1L, [||])>] | |
[<TestCase(2L, [|2L|])>] | |
[<TestCase(3L, [|3L|])>] | |
[<TestCase(4L, [|2L; 2L|])>] | |
[<TestCase(6L, [|3L; 2L|])>] | |
[<TestCase(8L, [|2L; 2L; 2L|])>] | |
[<TestCase(16L, [|2L; 2L; 2L; 2L|])>] | |
[<TestCase(773L, [|773L|])>] | |
let prime_factors_of_number number expect = | |
PrimeFactors().Of number |> should equal expect | |
[<TestCase(1L, 0L)>] | |
[<TestCase(4L, 1L)>] | |
[<TestCase(773L, 26L)>] | |
let step_count_of_number number stepCount = | |
PrimeFactorsMock().StepCount number |> should equal stepCount |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment