One branch recursion
parent
001756b7e3
commit
cb83365a24
@ -0,0 +1,9 @@
|
||||
package datastructures
|
||||
|
||||
func Factorial(n int) int {
|
||||
if n < 1 {
|
||||
return 1
|
||||
}
|
||||
|
||||
return n * Factorial(n-1)
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package datastructures
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFactorial(t *testing.T) {
|
||||
|
||||
cases := []struct {
|
||||
Name string
|
||||
Input int
|
||||
Expected int
|
||||
}{
|
||||
{
|
||||
"Factorial of 5",
|
||||
5,
|
||||
120,
|
||||
},
|
||||
{
|
||||
"Factorial of 10",
|
||||
10,
|
||||
3628800,
|
||||
},
|
||||
{
|
||||
"Factorial of 12",
|
||||
12,
|
||||
479001600,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range cases {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
got := Factorial(test.Input)
|
||||
|
||||
if got != test.Expected {
|
||||
t.Errorf("Error got: %d want: %d", got, test.Expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue