Skip to content

Instantly share code, notes, and snippets.

@obrassard
Created April 19, 2018 17:00
Show Gist options
  • Save obrassard/235817b423e0e61d0c1fc4b620b9e940 to your computer and use it in GitHub Desktop.
Save obrassard/235817b423e0e61d0c1fc4b620b9e940 to your computer and use it in GitHub Desktop.
public class FiboGenerator implements Iterable<BigInteger>, Iterator<BigInteger> {
BigInteger avantDernier = BigInteger.ZERO;
BigInteger avantAvantDernier = BigInteger.ONE ;
@Override
public Iterator<BigInteger> iterator() {
return this;
}
@Override
public boolean hasNext() {
return true;
}
@Override
public BigInteger next() {
BigInteger next = avantAvantDernier.add(avantDernier);
this.avantAvantDernier = this.avantDernier;
this.avantDernier = next;
return next;
}
}
// Execution :
public class Main {
public static void main(String[] args) {
for (BigInteger elt : new FiboGenerator()){
System.out.println(elt.toString());
if (elt.compareTo(new BigInteger("1000000")) == 1)
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment