Last active
June 6, 2020 01:01
-
-
Save andresabello/cd2669f35d2e365c86b5ecb48349729d to your computer and use it in GitHub Desktop.
Factorial functions in go. Differnence between recursive and array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
number := 8 | |
result := factorial(number) | |
travResult := traversal(number) | |
fmt.Println(result) | |
fmt.Println(travResult) | |
} | |
func factorial(number int) int { | |
if number < 2 { | |
return 1 | |
} | |
return number * factorial(number-1) | |
} | |
func traversal(number int) int { | |
result := number | |
for i := number - 1; i > 1; i-- { | |
result *= i | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment