poj Prime Gap

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 #include<algorithm>
 8 #include<map>
 9 #include<iomanip>
10 #include<climits>
11 #include<string.h>
12 #include<numeric>
13 #include<cmath>
14 #include<stdlib.h>
15 #include<vector>
16 #include<stack>
17 #include<set>
18 #define INF 1e7
19 #define MAXN 100010
20 #define maxn 1000010
21 #define Mod 1000007
22 #define N 1299800
23 using namespace std;
24 typedef long long LL;
25 
26 int prime[N];
27 int vis[N];
28 void run()
29 {
30     int k = 1;
31     for (LL i = 2; i <= N; ++i)
32         if (!vis[i]) {
33             prime[k++] = i;
34             for (LL j = i*i; j < N; j += i)
35                 vis[j] = 1;
36         }
37     /*for (int i = 1; i < 1300; ++i)
38         cout << prime[i] << " ";
39     cout << endl;*/
40 }
41 
42 int n;
43 int ans = 0;
44 
45 int main()
46 {
47     run();
48     while (cin >> n,n) {
49         ans = 2;
50         if (vis[n] == 0)
51             ans = 0;
52         else {
53             int tmp = n;
54             while (--tmp) 
55                 if (vis[tmp]) 
56                     ans++;
57                 else break;
58             tmp = n;
59             while (++tmp)
60                 if (vis[tmp])
61                     ans++;
62                 else break;
63         }
64         cout << ans << endl;
65     }
66     return 0;
67 }

 

posted @ 2015-03-26 18:07  UsedRose  阅读(89)  评论(0编辑  收藏  举报