Skip to content

Commit faeabe3

Browse files
Merge pull request #86 from ErikSchierboom/prime-factors
Add prime-factors exercise
2 parents c2df07b + 4b6a616 commit faeabe3

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"ocr-numbers",
5757
"simple-linked-list",
5858
"robot-simulator",
59+
"prime-factors",
5960
"diamond"
6061
],
6162
"deprecated": [

exercises/prime-factors/Example.fs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module PrimeFactors
2+
3+
open System
4+
5+
let possiblePrimes (number: int64): int64 list =
6+
[2L; 3L] @ [for n in 6L..6L..number do
7+
for k in [-1L; 1L] do
8+
yield n + k ]
9+
10+
let primeFactorsFor number =
11+
let rec loop factors (remainder: int64) (possibleFactors: int64 list) =
12+
match possibleFactors with
13+
| [] -> factors |> List.rev
14+
| factor::xs ->
15+
match remainder with
16+
| _ when remainder <= 1L -> factors |> List.rev
17+
| _ when remainder % factor = 0L -> loop (factor :: factors) (remainder / factor |> int64) possibleFactors
18+
| _ -> loop factors remainder (List.tail possibleFactors)
19+
20+
loop [] number (possiblePrimes number)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module PrimeFactorsTest
2+
3+
open NUnit.Framework
4+
5+
open PrimeFactors
6+
7+
[<Test>]
8+
let ``Test 1``() =
9+
Assert.That(primeFactorsFor 1L, Is.EqualTo([]))
10+
11+
[<Test>]
12+
[<Ignore("Remove to run test")>]
13+
let ``Test 2``() =
14+
Assert.That(primeFactorsFor 2L, Is.EqualTo([2]))
15+
16+
[<Test>]
17+
[<Ignore("Remove to run test")>]
18+
let ``Test 3``() =
19+
Assert.That(primeFactorsFor 3L, Is.EqualTo([3]))
20+
21+
[<Test>]
22+
[<Ignore("Remove to run test")>]
23+
let ``Test 4``() =
24+
Assert.That(primeFactorsFor 4L, Is.EqualTo([2; 2]))
25+
26+
[<Test>]
27+
[<Ignore("Remove to run test")>]
28+
let ``Test 6``() =
29+
Assert.That(primeFactorsFor 6L, Is.EqualTo([2; 3]))
30+
31+
[<Test>]
32+
[<Ignore("Remove to run test")>]
33+
let ``Test 8``() =
34+
Assert.That(primeFactorsFor 8L, Is.EqualTo([2; 2; 2]))
35+
36+
[<Test>]
37+
[<Ignore("Remove to run test")>]
38+
let ``Test 9``() =
39+
Assert.That(primeFactorsFor 9L, Is.EqualTo([3; 3]))
40+
41+
[<Test>]
42+
[<Ignore("Remove to run test")>]
43+
let ``Test 27``() =
44+
Assert.That(primeFactorsFor 27L, Is.EqualTo([3; 3; 3]))
45+
46+
[<Test>]
47+
[<Ignore("Remove to run test")>]
48+
let ``Test 625``() =
49+
Assert.That(primeFactorsFor 625L, Is.EqualTo([5; 5; 5; 5]))
50+
51+
[<Test>]
52+
[<Ignore("Remove to run test")>]
53+
let ``Test 901255``() =
54+
Assert.That(primeFactorsFor 901255L, Is.EqualTo([5; 17; 23; 461]))

0 commit comments

Comments
 (0)