forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial.go
55 lines (50 loc) · 1.13 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
// 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
// Iterative returns the iteratively brute forced factorial of n
func Iterative(n int) int {
result := 1
for i := 2; i <= n; i++ {
result *= i
}
return result
}
// Recursive This function recursively computes the factorial of a number
func Recursive(n int) int {
if n == 1 {
return 1
} else {
return n * Recursive(n-1)
}
}
// UsingTree This function finds the factorial of a number using a binary tree
func UsingTree(n int) int {
if n < 0 {
return 0
}
if n == 0 {
return 1
}
if n == 1 || n == 2 {
return n
}
return prodTree(2, n)
}
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)
}