sicily 1500. Prime Gap

Description

The sequence of n ? 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, 24, 25, 26, 27, 28 between 23 and 29 is a prime gap of length 6.

Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k.

Input

The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero.

Output

The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output.

Sample Input

 Copy sample input to clipboard

10
11
27
2
492170
0

Sample Output

4
0
6
0
114
分析:首先不可能输入每个数都去找一遍其周围的素数,我比较喜欢的是直接求出所需要的最大范围的素数,然后从这里面再进行下一步的计算。这里使用的是筛选法来得到素数集。
#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
    int prime[1299710];
    for (int i = 2; i != 1299710; ++i) {
        prime[i] = 1;
    }
    for (int i = 2; i != 1299710; ++i) {
        if (prime[i] == 1) {
            for (int j = 2 * i; j < 1299710; j += i)
                prime[j] = 0;
        }
    } // 筛选法找素数

    int num;
    while (cin >> num && num != 0) {
        int length = 0;
        if (prime[num] != 1) {
            int upperBound = 0, lowerBound = 0;
            for (lowerBound = num - 1; lowerBound >= 2; --lowerBound) {
                if (prime[lowerBound] == 1)
                    break;
            }
            for (upperBound = num + 1; upperBound < 1299710; ++upperBound) {
                if (prime[upperBound] == 1)
                    break;
            }
            length = upperBound - lowerBound;
        }
        cout << length << endl;
    }
    return 0;
}
时间: 2024-08-01 20:05:09

sicily 1500. Prime Gap的相关文章

1500. Prime Gap 11 月 11日

/*本篇为转载,在此申明,具体就是先设定从2以后所有的数都为质数,定为质数的数的倍数则不是质数,慢慢排除后面的数*/ #include<iostream>#include<cstring>using namespace std;const int N = 1300000;//第10万个数为1299709int prime[N];bool notPrime[N]; int main(){  memset(prime,0,sizeof(prime)); memset(notPrime,

poj 3518 Prime Gap

Prime Gap 1 #include<cmath> 2 #include<cstdio> 3 #include<algorithm> 4 #include<iostream> 5 #include<string> 6 #include<cstring> 7 #include<vector> 8 using namespace std; 9 #define max 100000 10 int prime[max+5];

POJ 3518 Prime Gap(素数题)

[题意简述]:输入一个数,假设这个数是素数就输出0,假设不是素数就输出离它近期的两个素数的差值,叫做Prime Gap. [分析]:这题过得非常险.由于我是打的素数表. 由于最大的素数是1299709,所以注意在打表时要使用long long.否则程序应该不能执行.注意这一点应该就能够了. 积累! // 2984K 235Ms #include<iostream> using namespace std; #define N 2000001 bool isprime[N]; long long

【UVA - 1644】Prime Gap(水题)

Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每行包含一个数n 若n为0,程序结束 Output 对于每个测试数据,输出一个答案占一行 Sample Input 10 11 27 2 492170 0 Sample Output 4 0 6 0 114 题目链接: https://vjudge.net/problem/UVA-1644 水题,先求

Sicily 1444: Prime Path(BFS)

题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 bool isPrime(int n){//素数判断 5 if(n == 2 || n == 3) return true; 6 else{ 7 int k = sqrt(n) + 1; 8 for(int i = 2; i < k; i

UVa 1644 Prime Gap (水题,暴力)

题意:给定一个数 n,求它后一个素数和前一个素数差. 析:先打表,再二分查找. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring&

UVa 1644 (筛素数 + 二分) Prime Gap

题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n,则直接输出0,否则输出它后一个素数与它本身的差值. 1 #include <cstdio> 2 #include <cmath> 3 4 const int maxn = 1299709; 5 const int maxp = 100000; 6 bool vis[maxn + 10

POJ 3518 Prime Gap(素数)

http://poj.org/problem?id=3518 题意: 给你一个数,如果该数是素数就输出0. 否则输出比这个数大的素数与比这个数小的素数的差值. 分析: 明显本题先要用筛选法求出130W(严格的话应该是求第100001个素数)以内的所有素数. 然后判断给的数是否是素数即可. 如果不是素数,那么就找出它在素数素组内的上界和下界,输出两个素数的差值即可. 筛选法求素数可见: http://blog.csdn.net/u013480600/article/details/41120083

poj 3518 Prime Gap 二分查找下界和素数筛法

/* 题意:输入有多组数据,每组数据一个n,如果n是素数,输出0否则输出离n最近的两个素数的积,第100000个素数是1299709,所有的素数都在这个范围内 思路:素数筛法加二分查找下界 */ #include<stdio.h> int a[1299720],pri[100005]; int Serch(int v)//二分查找下界 { int mid,x=0,y=100001; while(x<y) { mid=(y+x)/2; if(pri[mid]>=v) y=mid; e