Skip to content

Instantly share code, notes, and snippets.

@BenGoldberg1
Last active May 3, 2016 00:29
Show Gist options
  • Save BenGoldberg1/51123c54d2ae8e1f1d08 to your computer and use it in GitHub Desktop.
Save BenGoldberg1/51123c54d2ae8e1f1d08 to your computer and use it in GitHub Desktop.
Primes Thingy In C++
#include <iostream>
#include <deque>
typedef std::deque<int> mydeque;
void my_insert( mydeque & factors, int factor ) {
int where = factor, count = factors.size();
while( where < count && factors[where] ) where += factor;
if( where >= count ) factors.resize( where + 1 );
factors[ where ] = factor;
}
int main() {
mydeque primes;
mydeque factors;
int a_prime = 3, a_square_prime = 9, maybe_prime = 3;
factors.resize(3);
std::cout << "2 3 ";
while( maybe_prime <= 999 ) {
int factor = factors.front();
maybe_prime += 2;
if( factor ) {
my_insert( factors, factor );
} else if( maybe_prime < a_square_prime ) {
std::cout << maybe_prime << " ";
primes.push_back( maybe_prime );
} else {
my_insert( factors, a_prime );
a_prime = primes.front();
primes.pop_front();
a_square_prime = a_prime * a_prime;
}
factors.pop_front();
}
std::cout << std::endl;
return 0;
}
@BenGoldberg1
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment