Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- generator<int> primes(int start, int end){
- for ( int i = start; i < end; i++ ){
- if ( is_prime(i) ){
- yield i;
- }
- }
- }
- for ( auto prime : primes(100, 10000) ){
- cout << prime << " ";
- }
- struct unnamed_generator{
- int state;
- int local_i;
- int start;
- int end;
- unnamed_generator(int start, int end)
- : start(start)
- , end(end){
- }
- std::optional<int> resume(){
- switch ( state ){
- case 0:{
- local_i = start;
- state = 1;
- return resume();
- }break;
- case 1:{
- if ( local_i >= end ){
- state = 3;
- return resume();
- }
- if ( is_prime(local_i) ){
- state = 2;
- return local_i;
- }
- state = 2;
- return resume();
- }break;
- case 2:{
- local_i++;
- state = 1;
- return resume();
- }break;
- default:{
- return nullptr;
- }break;
- }
- }
- }
- unnamed_generator generator(100, 10000);
- while ( auto prime = generator.resume().get() ){
- cout << prime;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement