C ++ prime pair conjecture

 

 

 

 

My solution is to list all prime numbers from 2 to n first, and then calculate. To list all the prime numbers, a method called "Eratosine sieve method" was used.

The algorithm is here: https://www.sohu.com/a/252674565_614593

 1 #include <iostream>
 2 #include <vector>
 3 #include <cmath>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int n;
10     cin >> n;
11 
12     vector<int> arr;
13     arr.reserve(n+1);
14     for(int i = 2;i <= n;++i)
15     {
16         arr[i] = i;
17     }
18 
19     double a = sqrt(n);
20     //得到所有的素数
21     for(int i = 2;i <= a ;++i)
22     {
23         for(int j = 2;j*i <=n;j++)
24         {
25             arr[i*j] = 0;
26         }
27     }
28 
29     int time = 0;
30     for(int i = 2;i+2 <= n;++i)
31     {
32         if(arr[i+2]-arr[i] == 2)
33         {
34             time++;
35         }
36     }
37     cout << time;
38     return 0;
39 }

 

Guess you like

Origin www.cnblogs.com/apprendre-10-28/p/12732268.html