Skip to content

Instantly share code, notes, and snippets.

@hephyr
Created June 24, 2019 15:01
Show Gist options
  • Save hephyr/ae822323120f0e8eb6785a2008d6c6bd to your computer and use it in GitHub Desktop.
Save hephyr/ae822323120f0e8eb6785a2008d6c6bd to your computer and use it in GitHub Desktop.
package main
import "fmt"
func factorial_iter(product, counter, max_count int) int {
if counter > max_count {
return product
} else {
return factorial_iter(counter*product, counter+1, max_count)
}
}
func factorial2(n int) int {
return factorial_iter(1, 1, n)
}
func factorial(n int) int {
if n > 0 {
return factorial(n-1) * n
} else {
return 1
}
}
func main() {
result1 := factorial(6)
result2 := factorial2(6)
fmt.Println(result1, result2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment