-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
factorial.go
67 lines (60 loc) · 1.41 KB
/
factorial.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
// factorial.go
// description: Calculating factorial
// details:
// 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)
// author(s) [red_byte](https://github.com/i-redbyte)
// see factorial_test.go
// Package factorial describes algorithms Factorials calculations.
package factorial
import (
"errors"
)
var ErrNegativeArgument = errors.New("input argument must be non-negative integer")
// Iterative returns the iteratively brute forced factorial of n
func Iterative(n int) (int, error) {
if n < 0 {
return 0, ErrNegativeArgument
}
result := 1
for i := 2; i <= n; i++ {
result *= i
}
return result, nil
}
// Recursive This function recursively computes the factorial of a number
func Recursive(n int) (int, error) {
if n < 0 {
return 0, ErrNegativeArgument
}
if n <= 1 {
return 1, nil
}
prev, _ := Recursive(n - 1)
return n * prev, nil
}
// UsingTree This function finds the factorial of a number using a binary tree
func UsingTree(n int) (int, error) {
if n < 0 {
return 0, ErrNegativeArgument
}
if n == 0 {
return 1, nil
}
if n == 1 || n == 2 {
return n, nil
}
return prodTree(2, n), nil
}
func prodTree(l int, r int) int {
if l > r {
return 1
}
if l == r {
return l
}
if r-l == 1 {
return l * r
}
m := (l + r) / 2
return prodTree(l, m) * prodTree(m+1, r)
}