-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprime_factorization_test.go
52 lines (47 loc) · 1.25 KB
/
prime_factorization_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package numbertheory
import (
"fmt"
"testing"
)
func ExamplePrimeFactorization() {
var n uint64 = 51561510
r := PrimeFactorization(n)
fmt.Printf("The prime factorization of %v is %v.\n", n, r)
// Output:
// The prime factorization of 51561510 is [2 3 5 7 11 13 17 101].
}
func TestPrimeFactorization(t *testing.T) {
compare := func(t *testing.T, a []uint64, b []uint64) {
if len(a) != len(b) {
t.FailNow()
}
for k, n := range a {
if b[k] != n {
t.FailNow()
}
}
}
cases := map[uint64][]uint64{
0: {},
1: {},
2: {2},
2 * 2: {2, 2},
2 * 3 * 5: {2, 3, 5},
2 * 3 * 5 * 7 * 11 * 13 * 17: {2, 3, 5, 7, 11, 13, 17},
19 * 31 * 41 * 43: {19, 31, 41, 43},
2 * 3 * 5 * 7 * 11 * 13 * 17 * 101: {2, 3, 5, 7, 11, 13, 17, 101},
142_395_347 * 151: {151, 142_395_347},
}
for n, c := range cases {
n, c := n, c
t.Run(fmt.Sprintf("test %v", n), func(t *testing.T) {
t.Parallel()
compare(t, c, PrimeFactorization(n))
})
}
}
func BenchmarkPrimeFactorization(b *testing.B) {
for i := 0; i < b.N; i++ {
PrimeFactorization(uint64(i))
}
}