Skip to content

Instantly share code, notes, and snippets.

@calderonroberto
Last active April 9, 2016 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calderonroberto/0afdf815c6c5166f37e2ae0b09b8f7a4 to your computer and use it in GitHub Desktop.
Save calderonroberto/0afdf815c6c5166f37e2ae0b09b8f7a4 to your computer and use it in GitHub Desktop.
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