Skip to content

Instantly share code, notes, and snippets.

@esimov
Last active June 11, 2023 21:19
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save esimov/9622710 to your computer and use it in GitHub Desktop.
Save esimov/9622710 to your computer and use it in GitHub Desktop.
Factorial calculation in Go lang using three different methods: first traditionally, second with closure and third using memoization. The last method is the fastest between the three.
package main
import (
"fmt"
"time"
)
const LIM = 41
var facts [LIM]uint64
func main() {
fmt.Println("==================FACTORIAL==================")
start := time.Now()
for i:=uint64(0); i < LIM; i++ {
fmt.Printf("Factorial for %d is : %d \n", i, Factorial(uint64(i)))
}
end := time.Now()
fmt.Printf("Calculation finished in %s \n", end.Sub(start)) //Calculation finished in 2.0002ms
fmt.Println("==================FACTORIAL CLOSURE==================")
start = time.Now()
fact := FactorialClosure()
for i:=uint64(0); i < LIM; i++ {
fmt.Printf("Factorial closure for %d is : %d \n", uint64(i), fact(uint64(i)))
}
end = time.Now()
fmt.Printf("Calculation finished in %s \n", end.Sub(start)) //Calculation finished in 1ms
fmt.Println("==================FACTORIAL MEMOIZATION==================")
start = time.Now()
var result uint64 = 0
for i:=uint64(0); i < LIM; i++ {
result = FactorialMemoization(uint64(i))
fmt.Printf("The factorial value for %d is %d\n", uint64(i), uint64(result))
}
end = time.Now()
fmt.Printf("Calculation finished in %s\n", end.Sub(start)) // Calculation finished in 0ms
}
func Factorial(n uint64)(result uint64) {
if (n > 0) {
result = n * Factorial(n-1)
return result
}
return 1
}
func FactorialClosure() func(n uint64)(uint64) {
var a,b uint64 = 1, 1
return func(n uint64)(uint64) {
if (n > 1) {
a, b = b, uint64(n) * uint64(b)
} else {
return 1
}
return b
}
}
func FactorialMemoization(n uint64)(res uint64) {
if (facts[n] != 0) {
res = facts[n]
return res
}
if (n > 0) {
res = n * FactorialMemoization(n-1)
return res
}
return 1
}
@shaleh
Copy link

shaleh commented Aug 17, 2017

On go 1.8 there is almost no difference on my Linux box. In the memoization version you do not appear to be saving the output in facts and the recursive call is where the time is spent. I came across this gist when searching for 'golang memoization'.

You bigger problem is after 20! you reach the limits of uin64 and the code wraps and gives bogus answers.

@blaskovicz
Copy link

@tommyjcarpenter
Copy link

New to go here: how does the first syntax work with (result int)? Does that do some initialization magic?

@elmar-hinz
Copy link

FactorialClosure: a declared but not used

@slava-nikulin
Copy link

FactorialMemoization is not correct. If you want to use DP way, you need to fill facts array. But technically it will not has any differences with regular recursion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment