#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
// This function return vector of primes unsigned integer
vector<unsigned> getPrimes ( unsigned from, unsigned to ) { // both inclusive
vector<unsigned> primes; // declaration of result vector
bool *isprime = new bool[ to+1 ]; // creating sieve of Eratosthenes
memset ( isprime, 0xFF, to ); // setting all marks as true so every number now is prime
unsigned n = 2; // start from 2 becouse 1 obviously prime (or not prime - it's not a actual question here)
for ( ; n*n <= to; n++ ) { // cycle throught all integers (lesser or equal than 'to')
if ( isprime[ n ] ) { // if 'n' actualy is prime
for ( unsigned i = n*n; i <= to; i += n ) { // than cycle throght all divisible by 'n' greater than square of 'n'
isprime[ i ] = false; // and mark them as non-prime
}
if ( n > from ) { // also, if one included by range
primes.push_back ( n ); // push it back into vector
}
}
} // end of sieve
n = std::max ( n, from ); // and, after sieve
for ( ; n <= to; n++ ) { // we go to the end of our range
if ( isprime[ n ] ) { // in order to catch all remaining primes
primes.push_back ( n ); // and push it back into vector
}
}
return primes; // as final - return vector of primes
}
int main () {
int diff = 2;
auto primes = getPrimes ( 1, 997 );
for ( unsigned i = 0; i < 997; i++ ) {
if ( primes[ i + 1 ] - primes[ i ] == 2 ) {
std::cout << primes[ i ] << " and " << primes[ i + 1 ] << std::endl;
}
}
return 0;
}