mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 18:34:42 +01:00
20201119
This commit is contained in:
25
20201119/main.go
Normal file
25
20201119/main.go
Normal 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
83
20201119/main_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user