fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. // This function return vector of primes unsigned integer
  7. vector<unsigned> getPrimes ( unsigned from, unsigned to ) { // both inclusive
  8. vector<unsigned> primes; // declaration of result vector
  9. bool *isprime = new bool[ to+1 ]; // creating sieve of Eratosthenes
  10. memset ( isprime, 0xFF, to ); // setting all marks as true so every number now is prime
  11. unsigned n = 2; // start from 2 becouse 1 obviously prime (or not prime - it's not a actual question here)
  12. for ( ; n*n <= to; n++ ) { // cycle throught all integers (lesser or equal than 'to')
  13. if ( isprime[ n ] ) { // if 'n' actualy is prime
  14. for ( unsigned i = n*n; i <= to; i += n ) { // than cycle throght all divisible by 'n' greater than square of 'n'
  15. isprime[ i ] = false; // and mark them as non-prime
  16. }
  17.  
  18. if ( n > from ) { // also, if one included by range
  19. primes.push_back ( n ); // push it back into vector
  20. }
  21. }
  22. } // end of sieve
  23.  
  24. n = std::max ( n, from ); // and, after sieve
  25. for ( ; n <= to; n++ ) { // we go to the end of our range
  26. if ( isprime[ n ] ) { // in order to catch all remaining primes
  27. primes.push_back ( n ); // and push it back into vector
  28. }
  29. }
  30.  
  31. return primes; // as final - return vector of primes
  32. }
  33.  
  34. int main () {
  35. int diff = 2;
  36. auto primes = getPrimes ( 1, 997 );
  37. for ( unsigned i = 0; i < 997; i++ ) {
  38. if ( primes[ i + 1 ] - primes[ i ] == 2 ) {
  39. std::cout << primes[ i ] << " and " << primes[ i + 1 ] << std::endl;
  40. }
  41. }
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
3 and 5
5 and 7
11 and 13
17 and 19
29 and 31
41 and 43
59 and 61
71 and 73
101 and 103
107 and 109
137 and 139
149 and 151
179 and 181
191 and 193
197 and 199
227 and 229
239 and 241
269 and 271
281 and 283
311 and 313
347 and 349
419 and 421
431 and 433
461 and 463
521 and 523
569 and 571
599 and 601
617 and 619
641 and 643
659 and 661
809 and 811
821 and 823
827 and 829
857 and 859
881 and 883