I'm implementing some basic algorithms in Go as an introductory exercise. Here are four different algorithms to find the Nth Fibonacci number.
I'm looking for general feedback, but I'm specially interested in the Go idiom. Are the algorithms implemented idiomatically? If not, how can I correctly use the Go idiom to implement them?
Any other feedback you can think of is also welcome.
// Algorithms to calculate the nth fibonacci number
package main
import (
"fmt"
"math"
)
func main() {
for i := 0; i <= 20; i++ {
fmt.Println(fibonacci(i))
fmt.Println(fibonacciRecursive(i))
fmt.Println(fibonacciTail(i))
fmt.Println(fibonacciBinet(i))
println()
}
}
// Iterative
func fibonacci(n int) int {
current, prev := 0, 1
for i := 0; i < n; i++ {
current, prev = current + prev, current
}
return current
}
// Recursive
func fibonacciRecursive(n int) int {
if n < 2 {
return n
}
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2)
}
// Tail recursion
func fibonacciTail(n int) int {
return tailHelper(n, 1, 0)
}
func tailHelper(term, val, prev int) int {
if term == 0 {
return prev
}
if term == 1 {
return val
}
return tailHelper(term - 1, val + prev, val)
}
// Analytic (Binet's formula)
func fibonacciBinet(num int) int {
var n float64 = float64(num);
return int( ((math.Pow(((1 + math.Sqrt(5)) / 2), n) - math.Pow(1 - ((1 + math.Sqrt(5)) / 2), n)) / math.Sqrt(5)) + 0.5 )
}