Created
April 19, 2018 17:00
-
-
Save obrassard/235817b423e0e61d0c1fc4b620b9e940 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
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