-
Notifications
You must be signed in to change notification settings - Fork 7
/
polygonal_number_test.go
187 lines (168 loc) · 3.93 KB
/
polygonal_number_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package numbertheory
import (
"fmt"
"testing"
)
func ExamplePolygonalNumber() {
var n uint64 = 30
p, _ := PolygonalNumber(3, n)
fmt.Printf("The %vth triangular number is %v.\n", n, p)
p, _ = PolygonalNumber(4, n)
fmt.Printf("The %vth square number is %v.\n", n, p)
p, _ = PolygonalNumber(5, n)
fmt.Printf("The %vth pentagonal number is %v.\n", n, p)
// Output:
// The 30th triangular number is 465.
// The 30th square number is 900.
// The 30th pentagonal number is 1335.
}
func TestPolygonalNumber(t *testing.T) {
compare := func(t *testing.T, expected uint64, actual uint64) {
if expected != actual {
t.Logf("expected %v, got %v", expected, actual)
t.FailNow()
}
}
cases := []struct {
s uint64
n uint64
expected uint64
err error
}{
{2, 0, 0, fmt.Errorf("s must be >= 3")},
{3, 0, 0, nil},
{3, 1, 1, nil},
{3, 2, 3, nil},
{3, 3, 6, nil},
{4, 4, 16, nil},
{5, 5, 35, nil},
{6, 7, 91, nil},
}
for _, c := range cases {
c := c
t.Run(fmt.Sprintf("test s=%v, n=%v", c.s, c.n), func(t *testing.T) {
t.Parallel()
a, err := PolygonalNumber(c.s, c.n)
if c.err != nil {
if err == nil {
t.FailNow()
}
} else {
if err != nil {
t.FailNow()
}
compare(t, c.expected, a)
}
})
}
}
func ExamplePolygonalRoot() {
var x uint64 = 465
p, _ := PolygonalRoot(3, x)
fmt.Printf("The triangular root of %v is %v.\n", x, p)
x = 900
p, _ = PolygonalRoot(4, x)
fmt.Printf("The square root of %v is %v.\n", x, p)
x = 1335
p, _ = PolygonalRoot(5, x)
fmt.Printf("The pentagonal root of %v is %v.\n", x, p)
// Output:
// The triangular root of 465 is 30.
// The square root of 900 is 30.
// The pentagonal root of 1335 is 30.
}
func TestPolygonalRoot(t *testing.T) {
compare := func(t *testing.T, expected float64, actual float64) {
if expected != actual {
t.Logf("expected %v, got %v", expected, actual)
t.FailNow()
}
}
cases := []struct {
s uint64
x uint64
expected float64
err error
}{
{2, 0, 0, fmt.Errorf("s must be >= 3")},
{3, 6, 3, nil},
{4, 16, 4, nil},
{5, 35, 5, nil},
{6, 91, 7, nil},
}
for _, c := range cases {
c := c
t.Run(fmt.Sprintf("test s=%v, x=%v", c.s, c.x), func(t *testing.T) {
t.Parallel()
a, err := PolygonalRoot(c.s, c.x)
if c.err != nil {
if err == nil {
t.FailNow()
}
} else {
if err != nil {
t.FailNow()
}
compare(t, c.expected, a)
}
})
}
}
func ExamplePolygonalSides() {
var x uint64 = 465
p := PolygonalSides(30, x)
fmt.Printf("The 30th polygonal number that has a value of %v has %v sides.\n", x, p)
x = 900
p = PolygonalSides(30, x)
fmt.Printf("The 30th polygonal number that has a value of %v has %v sides.\n", x, p)
x = 1335
p = PolygonalSides(30, x)
fmt.Printf("The 30th polygonal number that has a value of %v has %v sides.\n", x, p)
// Output:
// The 30th polygonal number that has a value of 465 has 3 sides.
// The 30th polygonal number that has a value of 900 has 4 sides.
// The 30th polygonal number that has a value of 1335 has 5 sides.
}
func TestPolygonalSides(t *testing.T) {
compare := func(t *testing.T, expected float64, actual float64) {
if expected != actual {
t.Logf("expected %v, got %v", expected, actual)
t.FailNow()
}
}
cases := []struct {
n uint64
x uint64
expected float64
err error
}{
{3, 6, 3, nil},
{4, 16, 4, nil},
{5, 35, 5, nil},
{7, 91, 6, nil},
{5, 115, 13, nil},
}
for _, c := range cases {
c := c
t.Run(fmt.Sprintf("testn=%v, x=%v", c.n, c.x), func(t *testing.T) {
t.Parallel()
a := PolygonalSides(c.n, c.x)
compare(t, c.expected, a)
})
}
}
func BenchmarkPolygonalNumber(b *testing.B) {
for i := 0; i < b.N; i++ {
PolygonalNumber(3, i)
}
}
func BenchmarkPolygonalRoot(b *testing.B) {
for i := 0; i < b.N; i++ {
PolygonalRoot(3, i)
}
}
func BenchmarkPolygonalSides(b *testing.B) {
for i := 0; i < b.N; i++ {
PolygonalSides(3+i, 3+i)
}
}