File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ # Fibonacci sequence
2+
3+ Fibonacci sequence implementation in Go (Golang).
4+
5+ ` Time complexity: O(n) `
6+
7+ ` Space complexity: O(1) `
8+
9+ ``` Bash
10+ $ go test -v ./...
11+ === RUN TestCase1
12+ --- PASS: TestCase1 (0.00s)
13+ === RUN TestCase2
14+ --- PASS: TestCase2 (0.00s)
15+ PASS
16+ ok github.com/mxssl/FibonacciGolang 0.005s
17+ ```
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "fmt"
4+
5+ func main () {
6+ fmt .Println (getNthFib (6 ))
7+ }
8+
9+ func getNthFib (n int ) int {
10+ lastTwo := []int {0 , 1 }
11+ counter := 3
12+ for counter <= n {
13+ nextFib := lastTwo [0 ] + lastTwo [1 ]
14+ lastTwo = []int {lastTwo [1 ], nextFib }
15+ counter ++
16+ }
17+ if n > 1 {
18+ return lastTwo [1 ]
19+ }
20+ return lastTwo [0 ]
21+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "reflect"
5+ "testing"
6+ )
7+
8+ func TestCase1 (t * testing.T ) {
9+ expected := 0
10+ output := getNthFib (1 )
11+ if ! reflect .DeepEqual (output , expected ) {
12+ t .Fail ()
13+ }
14+ }
15+
16+ func TestCase2 (t * testing.T ) {
17+ expected := 1597
18+ output := getNthFib (18 )
19+ if ! reflect .DeepEqual (output , expected ) {
20+ t .Fail ()
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments