Skip to content

Commit

Permalink
Merge pull request #480 from vaskoz/day233
Browse files Browse the repository at this point in the history
Day233
  • Loading branch information
vaskoz committed Apr 12, 2019
2 parents 4afb02d + ded3127 commit e1b17d6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,4 @@ problems from
* [Day 230](https://github.com/vaskoz/dailycodingproblem-go/issues/473)
* [Day 231](https://github.com/vaskoz/dailycodingproblem-go/issues/475)
* [Day 232](https://github.com/vaskoz/dailycodingproblem-go/issues/477)
* [Day 233](https://github.com/vaskoz/dailycodingproblem-go/issues/479)
27 changes: 27 additions & 0 deletions day233/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package day233

import "errors"

var errOverflow = errors.New("overflow occurred")

// ErrOverflow represents an overflow error.
func ErrOverflow() error {
return errOverflow
}

// FastFibonnaci runs in O(N) time with O(1) space.
// Returns an error if overflow occurs.
func FastFibonnaci(n uint) (uint, error) {
if n < 2 {
return n, nil
}
first, second := uint(0), uint(1)
var err error
for i := uint(1); i < n; i++ {
first, second = second, first+second
if second < first {
return 0, errOverflow
}
}
return second, err
}
34 changes: 34 additions & 0 deletions day233/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package day233

import "testing"

var testcases = []struct {
n, fib uint
err error
}{
{0, 0, nil},
{1, 1, nil},
{2, 1, nil},
{12, 144, nil},
{28, 317811, nil},
{44, 701408733, nil},
{87, 679891637638612258, nil},
{100, 0, ErrOverflow()},
}

func TestFastFibonnaci(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result, err := FastFibonnaci(tc.n); result != tc.fib || err != tc.err {
t.Errorf("Given n=%d, expected (%d,%v), got (%d,%v)", tc.n, tc.fib, tc.err, result, err)
}
}
}

func BenchmarkFastFibonnaci(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
FastFibonnaci(tc.n) //nolint
}
}
}

0 comments on commit e1b17d6

Please sign in to comment.