Prime Gap
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7392   Accepted: 4291

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

10
11
27
2
492170
0

Sample Output

4
0
6
0
114

Source

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 1300000
bool hash[maxn];
void inithash()
{
int i,j;
for(j=; j<maxn; j+=)
hash[j]=;
for(i=; i<; i+=)
if(!hash[i])
for(j=i*i; j<maxn; j+=i)
hash[j]=;
}
int isprime(int N)
{
if(!hash[N])
return true;
return false;
}
int main()
{
inithash();
int n;
while(scanf("%d",&n),n)
{
int i=n;
while()
{
if(isprime(i))
break;
i++;
}
int j=n;
while()
{
if(isprime(j))
break;
j--; }
printf("%d\n",i-j);
}
return ;
}

poj 3518 Prime Gap的更多相关文章

  1. POJ 3518 Prime Gap(素数)

    POJ 3518 Prime Gap(素数) id=3518">http://poj.org/problem? id=3518 题意: 给你一个数.假设该数是素数就输出0. 否则输出比 ...

  2. POJ 3518 Prime Gap(素数题)

    [题意简述]:输入一个数,假设这个数是素数就输出0,假设不是素数就输出离它近期的两个素数的差值,叫做Prime Gap. [分析]:这题过得非常险.由于我是打的素数表. 由于最大的素数是1299709 ...

  3. POJ 3581 Prime Gap(二分)

    Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11009 Accepted: 6298 Descriptio ...

  4. 【UVA - 1644 / POJ - 3518】Prime Gap(水题)

    Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每 ...

  5. [暑假集训--数论]poj3518 Prime Gap

    The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...

  6. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  7. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  8. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  9. sicily 1500. Prime Gap

    Description The sequence of n ? 1 consecutive composite numbers (positive integers that are not prim ...

随机推荐

  1. JDBC的步骤

    使用jdbc步骤 a.导入数据库厂商提供的驱动程序(导入jar包) b.加载驱动程序 Class.forName("驱动程序类") c.获得连接 Connection conn=D ...

  2. 文件处理工具 gif合成工具 文件后缀批量添加工具 文件夹搜索工具 重复文件查找工具 网页图片解析下载工具等

    以下都是一些简单的免费分享的工具,技术支持群:592132877,提供定制化服务开发. Gif动图合成工具 主要功能是扫描指定的文件夹里的所有zip文件,然后提取Zip文件中的图片,并合成一张gif图 ...

  3. libcurl 错误码总结

    下载出现这种错误(Requested range was not delivered by the server  ),说明是重复下载,删掉本地的再下载就不会出现了

  4. iOS开发-UITextView文字排版

    UITextView文本排版 1.配置NSMutableParagraphStyle NSMutableParagraphStyle *MParaStyle = [[NSMutableParagrap ...

  5. Vue Devtools--vue调式工具

    当浏览器控制台出现:Download the Vue Devtools extension for a better development experience: 1:安装 地址:https://g ...

  6. 安装vmware exsi 6.0(自制虚拟服务器)

    安装准备:2枚U盘.每个U盘大于2G.或者1枚光盘外加一枚U盘. exsi是一个虚拟服务容器.是一个专门运行虚拟的服务器系统. 关于服务的要求.由于exsi是专业的虚拟服务容器.所以服务器要求比较苛刻 ...

  7. jquery动态增加或删除tr和td【实际项目】

    难点: (1)动态增加.删除tr和td (2)每天tr和td都有下标,且下标要动态变化, (3)tr和td为什么下标不能随便写,原因是此处需要把所有tr中的数据以list的形式发送到后台对象中,所有每 ...

  8. You're Given a String...

    You're given a string of lower-case Latin letters. Your task is to find the length of its longest su ...

  9. flask第十三篇——url注意事项

    先和大家说一下常用的请求方法 get:只是从服务器获取数据,不会对服务器的状态或数据产生任何影响: get方法的参数是放在URL中传递的. post:会对服务器的状态或数据产生影响:通过body形式进 ...

  10. 【angularJS】Service服务

    AngularJS 中的服务是一个函数或对象.可以创建自己的服务,或使用内建服务. 内置服务 AngularJS 内建了30 多个服务. 1.  $location 服务,它可以返回当前页面的 URL ...