Skip to content

Instantly share code, notes, and snippets.

@defHLT
Created June 1, 2015 23:12
Show Gist options
  • Save defHLT/8eccbefd5eee5247d984 to your computer and use it in GitHub Desktop.
Save defHLT/8eccbefd5eee5247d984 to your computer and use it in GitHub Desktop.
Fib generator
import java.util.Iterator;
class HelloWorldApp {
public static void main(String[] args) {
Iterable<Integer> fibo = new Iterable<Integer>() {
@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int a = 0;
int b = 1;
int total;
@Override
public boolean hasNext() {
return true;
}
@Override
public Integer next() {
int old_a = a;
a = b;
b = old_a + b;
return old_a;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
Iterator<Integer> it = fibo.iterator();
int n = 0;
while (n++ < 25) {
System.out.print(it.next() + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment