Skip to content

Instantly share code, notes, and snippets.

@moogzy
Last active November 15, 2016 04:24
Show Gist options
  • Save moogzy/834eaf35ec2b493758a160b00835e7a2 to your computer and use it in GitHub Desktop.
Save moogzy/834eaf35ec2b493758a160b00835e7a2 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "math"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func(int) int {
return func(x int) int {
// Use the Golden ratio formula to calculate the fibonacci value given an integer
// position. (i.e position 6 == value of 8)
//
// http://sahandsaba.com/five-ways-to-calculate-fibonacci-numbers-with-python-code.html
a := math.Pow(1.618034, float64(x))
b := math.Pow((1 - 1.618034), float64(x))
y := (a - b) / math.Sqrt(float64(5))
return int(y)
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f(i))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment