Created
December 16, 2018 20:34
-
-
Save patrickbrandt/657e87b916477c129e4b652d98ba8a81 to your computer and use it in GitHub Desktop.
Fibonacci in Go
This file contains 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" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
// this is my solve to https://tour.golang.org/moretypes/26 | |
func fibonacci() func() int { | |
next := -1 | |
sum := 0 | |
current := 0 | |
return func() int { | |
if (next < 1 ) { | |
next = next + 1 | |
return next | |
} | |
sum = current + next | |
current = next | |
next = sum | |
return sum | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
better implementation here: https://hackernoon.com/aws-lambda-go-vs-node-js-performance-benchmark-1c8898341982
(JavaScript version)