Created
August 3, 2019 12:59
-
-
Save zolotyh/87a73aecae7a303fe3783d6c55fc809c 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
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