Last active
April 9, 2016 16:04
-
-
Save calderonroberto/0afdf815c6c5166f37e2ae0b09b8f7a4 to your computer and use it in GitHub Desktop.
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" | |
"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