-
Notifications
You must be signed in to change notification settings - Fork 7
/
totient_test.go
78 lines (69 loc) · 1.31 KB
/
totient_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package numbertheory
import (
"fmt"
"testing"
)
func ExampleTotient() {
r := Totient(60)
fmt.Printf("Euler's totient of 60 is %v.\n", r)
// Output:
// Euler's totient of 60 is 16.
}
func TestTotient(t *testing.T) {
compare := func(t *testing.T, a uint64, b uint64) {
if a != b {
t.Logf("expected %v, got %v", a, b)
t.FailNow()
}
}
cases := map[uint64]uint64{
12: 4,
13: 12,
38: 18,
41: 40,
60: 16,
}
for n, expected := range cases {
n, expected := n, expected
t.Run(fmt.Sprintf("test %v", n), func(t *testing.T) {
t.Parallel()
compare(t, expected, Totient(n))
})
}
}
func ExampleTotientK() {
r := TotientK(60, 2)
fmt.Printf("The J₂ of 60 is %v.", r)
// Output:
// The J₂ of 60 is 2304.
}
func TestTotientK2(t *testing.T) {
compare := func(t *testing.T, a uint64, b uint64) {
if a != b {
t.Logf("expected %v, got %v", a, b)
t.FailNow()
}
}
cases := map[uint64]uint64{
17: 288,
38: 1080,
60: 2304,
}
for n, expected := range cases {
n, expected := n, expected
t.Run(fmt.Sprintf("test %v", n), func(t *testing.T) {
t.Parallel()
compare(t, expected, TotientK(n, 2))
})
}
}
func BenchmarkTotient(b *testing.B) {
for i := 0; i < b.N; i++ {
Totient(i)
}
}
func BenchmarkTotientK(b *testing.B) {
for i := 0; i < b.N; i++ {
TotientK(i, 2)
}
}