This commit is contained in:
VicRen
2020-11-19 09:32:16 +08:00
parent cbc637da8b
commit f68ebc91b6
2 changed files with 108 additions and 0 deletions

25
20201119/main.go Normal file
View File

@@ -0,0 +1,25 @@
package main
import (
"fmt"
)
func main() {
fmt.Println(multiple(9, 9))
}
func multiple(n, m uint64) uint64 {
if m == 1 {
return n
}
return n + multiple(n, m-1)
}
func multiple2(n, m uint64) uint64 {
sum := n
m--
if m > 0 {
sum += multiple2(n, m)
}
return sum
}

83
20201119/main_test.go Normal file
View File

@@ -0,0 +1,83 @@
package main
import "testing"
func Test_multiple(t *testing.T) {
type args struct {
n uint64
m uint64
}
tests := []struct {
name string
args args
want uint64
}{
{
"example_1",
args{1, 10},
10,
},
{
"example_2",
args{3, 4},
12,
},
{
"example_3",
args{100, 100},
10000,
},
{
"example_4",
args{300, 100},
30000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := multiple(tt.args.n, tt.args.m); got != tt.want {
t.Errorf("multiple() = %v, want %v", got, tt.want)
}
})
}
}
func Test_multiple2(t *testing.T) {
type args struct {
n uint64
m uint64
}
tests := []struct {
name string
args args
want uint64
}{
{
"example_1",
args{1, 10},
10,
},
{
"example_2",
args{3, 4},
12,
},
{
"example_3",
args{100, 100},
10000,
},
{
"example_4",
args{300, 100},
30000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := multiple2(tt.args.n, tt.args.m); got != tt.want {
t.Errorf("multiple2() = %v, want %v", got, tt.want)
}
})
}
}