There are numbers from 1, 2, 3 ... N (N <= 500000). Write a program that distributes the numbers in pairs so that their sum is a prime number. I do not know how to make this distribution correctly, tell me. I wrote as I know, but the campaign is not at all that.

#include "stdafx.h" #include <iostream> using namespace std; int main() { setlocale(LC_ALL, "rus"); int i,a; int A[500000]; for (i = 1; i <= 500000; i++) a = A[i]; a += A[i + 1]; if ((a%a == 1) && (a % 1 == 1)) cout << a; system("pause"); } 

Closed due to the fact that off-topic participants ߊߚߤߘ , Igor , Akina , Nicolas Chabanovsky Mar 7 '18 at 15:00 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    (a%a == 1) - give an example when this condition is true. The same is for (a % 1 == 1) . - Igor
  • I would like to make a check on whether a simple number or not. Sorry I do not understand. - Alex
  • the program will not even compile. and still see no point in the for loop. most likely, it should not end ; - Senior Pomidor
  • Write a program that distributes the numbers in pairs so that their sum is a prime number. What should ALL numbers be used? it's NP ... - Akina
  • is the factorial complexity of thousands?), or do we need the first ones? .. or maybe there is a mathematical solution .. - J. Doe

1 answer 1

 bool isprime(int thenumber) { int isitprime = 1, loop; for (loop = 3; (isitprime) && (loop<(sqrt(thenumber) + 1)); loop += 2) isitprime = (thenumber % loop); return(isitprime); } #include <algorithm> #include <vector> void main(int argc, char**argv) { int k = 0, n = argc > 1 ? atoi(argv[1]) : 500000; std::vector<int> x, primes = { 2 }; for (int i = 3; i < n * 2; i += 2) if (isprime(i)) primes.push_back(i); for (int i = 0; i < n; i++) x.push_back(i + 1); for (auto i = x.begin(), end = x.end(); i < end; i++) { for (auto j = i + 1; j < end; j++) if (std::binary_search(primes.begin(), primes.end(), *i + *j)) printf("%7d %7d\n", *i, *j), std::iter_swap(++i, j), j = end - 1, k++; } printf("%d\n", k * 2); } 

for more than 10,000, extra numbers remain, well, maybe it should be (ツ)

  • @ARHovsepyan - the words of the one who already understands everything and knows, for us who are not understanding, everything is useful and helps), unless of course copy-paste will not be - J. Doe
  • I agree, but this is not the case at all _ here at least a person needs at least elementary knowledge, and not ready-made code - AR Hovsepyan