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

给个x,如果x夹在两个质数a,b之间,求b-a,否则输出0

在筛法的时候预处理下距离就好

 #include<cstdio>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL n;
bool mk[];
int p[],len;
int ans[];
inline LL LLabs(LL a){return a<?-a:a;}
inline void getp()
{
memset(ans,-,sizeof(ans));
for (int i=;i<=;i++)
{
if (!mk[i])
{
p[++len]=i;
ans[i]=;
for (int j=*i;j<=;j+=i)mk[j]=;
}else ans[i]=ans[i-]+;
}
for (int i=;i>=;i--)
{
if (!ans[i])continue;
ans[i]=max(ans[i],ans[i+]);
}
}
int main()
{
getp();
ans[]=-;
while (~scanf("%lld",&n)&&n)printf("%d\n",ans[n]?ans[n]+:);
}

poj 3518

[暑假集训--数论]poj3518 Prime Gap的更多相关文章

  1. [暑假集训--数论]poj1365 Prime Land

    Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...

  2. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  3. [暑假集训--数论]hdu2136 Largest prime factor

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  4. [暑假集训--数论]poj2262 Goldbach's Conjecture

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...

  5. [暑假集训--数论]poj2909 Goldbach's Conjecture

    For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...

  6. [暑假集训--数论]poj2773 Happy 2006

    Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD ...

  7. [暑假集训--数论]hdu1019 Least Common Multiple

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  8. [暑假集训--数论]poj2115 C Looooops

    A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...

  9. [暑假集训--数论]poj2034 Anti-prime Sequences

    Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...

随机推荐

  1. java基础编程——获取栈中的最小元素

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)).   题目代码 /** * Created by YuKai Fan on 2018/9 ...

  2. 51nod——2476 小b和序列(预处理 思维)

    对于每一个元素,预处理出它作为最小值,两边可以作用到的最大位置.比如下标∈[0,8]的这个数组:1 8 6 2 5 4 3 8 7,1可以作用到所有区间,2可以作用到区间[1,8],第一个8可以作用到 ...

  3. php扩展开发-函数

    我们首先找到快速上手文章里面关于函数定义的代码,以此说明然后开发PHP的函数 //php_myext.h PHP_FUNCTION(myext_hello);//函数申明,所有在myext.c文件定义 ...

  4. 后缀数组的使用心得——POJ2774 最长连续公共子串

    对于这道题,将两个字符串直接合并成为一个字符串,分别记录连个字符串结束的位置. 首先,应用黑暗圣典的模板,我们可以顺利得到height,rank,sa三个数组. 之后直接扫描1-n所有的位置,选出来一 ...

  5. IQueryable与IEnumerable区别

    前者可以延迟加载,即执行完后不马上执行数据库语句,用到再加载.

  6. vba通过函数调用Winrar压缩软件压缩文件

    Dim site As String        Dim FolderName As String        Dim FolderName1 As String        Dim Rarex ...

  7. Nodejs-文件流

    1.什么是流? 流是程序输入输出的一个连续的字节序列. 有文件流,网络流,设备(例如鼠标,键盘,磁盘,调制解调器和打印机)的输入输出都是用流来处理的. 任何数据的最根本表现形式都是二进制. 读取文件 ...

  8. python 学习分享-常用模块篇

    模块 就是前人给你造的轮子,你开车就好!!! 常用模块有: time模块 random模块 os模块 sys模块 shutil模块 json  &  picle模块 shelve模块 xml处 ...

  9. python 笔试总结

    1.对比两种函数对应结果 def fn(x): if x>0: print(x) fn(x-1) ****结果****** 3 2 1 $$$$$$另外一种$$$$$$$$$ def fn(x) ...

  10. git基础之常用操作

    一.版本提交: (1)git add 文件名 (2)git commit -m "版本提交信息" 注:git分两个区:工作区+版本库 在电脑中看到的文件夹就是工作区 有一个隐藏的. ...