Skip to content

Instantly share code, notes, and snippets.

@zolotyh
Created August 3, 2019 12:59
Show Gist options
  • Save zolotyh/87a73aecae7a303fe3783d6c55fc809c to your computer and use it in GitHub Desktop.
Save zolotyh/87a73aecae7a303fe3783d6c55fc809c to your computer and use it in GitHub Desktop.
Iterable<BigInt> getFibIterator() sync* {
var initial = true;
BigInt prev = null;
BigInt beforePrev = BigInt.from(1);
while(true){
if(initial && prev == null){
initial = false;
prev = BigInt.from(0);
yield BigInt.from(0);
} else {
final temp = prev + beforePrev;
prev = beforePrev;
beforePrev = temp;
yield prev;
}
}
}
class FibIterable extends Iterable {
final _iterator = FibonacciIterator();
@override
Iterator get iterator => _iterator;
}
class FibonacciIterator implements Iterator<BigInt> {
bool _initial = true;
BigInt _prev = null;
BigInt _beforePrev = BigInt.from(1);
bool moveNext() {
if (_initial && _prev == null) {
_initial = false;
_prev = BigInt.from(0);
} else {
final BigInt _temp = _prev + _beforePrev;
_prev = _beforePrev;
_beforePrev = _temp;
}
return true;
}
BigInt get current => _prev;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment