Skip to content

Commit 1fce86c

Browse files
authored
Add calculating factorial (#352)
1 parent d700f6a commit 1fce86c

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

math/factorial/factorial.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// factorial.go
2+
// description: Calculating factorial
3+
// details:
4+
// The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n - [Factorial](https://en.wikipedia.org/wiki/Factorial)
5+
// author(s) [red_byte](https://github.com/i-redbyte)
6+
// see factorial_test.go
7+
8+
// Package factorial describes algorithms Factorials calculations.
9+
package factorial
10+
11+
func BruteForceFactorial(n int) int {
12+
result := 1
13+
for i := 2; i <= n; i++ {
14+
result *= i
15+
}
16+
return result
17+
}
18+
19+
func RecursiveFactorial(n int) int {
20+
if n == 1 {
21+
return 1
22+
} else {
23+
return n * RecursiveFactorial(n-1)
24+
}
25+
}
26+
27+
func CalculateFactorialUseTree(n int) int {
28+
if n < 0 {
29+
return 0
30+
}
31+
if n == 0 {
32+
return 1
33+
}
34+
if n == 1 || n == 2 {
35+
return n
36+
}
37+
return prodTree(2, n)
38+
}
39+
40+
func prodTree(l int, r int) int {
41+
if l > r {
42+
return 1
43+
}
44+
if l == r {
45+
return l
46+
}
47+
if r-l == 1 {
48+
return l * r
49+
}
50+
m := (l + r) / 2
51+
return prodTree(l, m) * prodTree(m+1, r)
52+
}

math/factorial/factorial_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// factorial_test.go
2+
// description: Test for calculating factorial
3+
// author(s) [red_byte](https://github.com/i-redbyte)
4+
// see factorial.go
5+
6+
package factorial
7+
8+
import "testing"
9+
10+
func getTests() []struct {
11+
name string
12+
n int
13+
result int
14+
} {
15+
var tests = []struct {
16+
name string
17+
n int
18+
result int
19+
}{
20+
{"5!", 5, 120},
21+
{"3!", 3, 6},
22+
{"6!", 6, 720},
23+
{"12!", 12, 479001600},
24+
{"1!", 1, 1},
25+
}
26+
return tests
27+
}
28+
29+
func TestBruteForceFactorial(t *testing.T) {
30+
tests := getTests()
31+
for _, tv := range tests {
32+
t.Run(tv.name, func(t *testing.T) {
33+
result := BruteForceFactorial(tv.n)
34+
if result != tv.result {
35+
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.result, result)
36+
}
37+
})
38+
}
39+
}
40+
41+
func TestRecursiveFactorial(t *testing.T) {
42+
tests := getTests()
43+
for _, tv := range tests {
44+
t.Run(tv.name, func(t *testing.T) {
45+
result := RecursiveFactorial(tv.n)
46+
if result != tv.result {
47+
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.result, result)
48+
}
49+
})
50+
}
51+
}
52+
53+
func TestCalculateFactorialUseTree(t *testing.T) {
54+
tests := getTests()
55+
for _, tv := range tests {
56+
t.Run(tv.name, func(t *testing.T) {
57+
result := CalculateFactorialUseTree(tv.n)
58+
if result != tv.result {
59+
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.result, result)
60+
}
61+
})
62+
}
63+
}
64+
65+
func BenchmarkBruteForceFactorial(b *testing.B) {
66+
for i := 0; i < b.N; i++ {
67+
BruteForceFactorial(10)
68+
}
69+
}
70+
71+
func BenchmarkRecursiveFactorial(b *testing.B) {
72+
for i := 0; i < b.N; i++ {
73+
RecursiveFactorial(10)
74+
}
75+
}
76+
77+
func BenchmarkCalculateFactorialUseTree(b *testing.B) {
78+
for i := 0; i < b.N; i++ {
79+
RecursiveFactorial(10)
80+
}
81+
}

0 commit comments

Comments
 (0)