Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"math"
"time"
)
func fibonacci(n int) int {
a := 0
b := 1
// Iterate until desired position in sequence.
for i := 0; i < n; i++ {
// Use temporary variable to swap values.
temp := a
a = b
b = temp + a
}
return a
}
func main() {
start := time.Now()
for n := 0; n < 1000000; n++ {
fibonacci(n)
// powers of 10
if math.Remainder(math.Log10(float64(n)), 1) == 0 {
fmt.Printf("Elapsed time for %d %s\n", n, time.Since(start))
}
}
end := time.Since(start)
fmt.Printf("TOTAL Elapsed time %s\n", end)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.