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控制语句例题,for循环语句,if条件语句等,Scanner类与Random类,Math.max()方法

    例题:编写程序,生成5个1至10之间的随机整数,并打印结果到控制台 import java.util.Random;class demo09 { public static void main(Str ...

  2. 转:Python字典与集合操作总结

    转自:http://blog.csdn.net/business122/article/details/7537014 一.创建字典 方法①: >>> dict1 = {} > ...

  3. python的对数

    python的对数 首先要导入 math 模块: import math import numpy as np math.log(8,2),此为以2为底8的对数 等于 math.log2(8); 等于 ...

  4. C/C++程序基础 (五)位运算

    C++中四种转换运算符的区分 const_cast 修改const和volatile属性 reinterpret_cast 指针间类型转换或者指针和整形的转换.二进制重新翻译. static_cast ...

  5. 第六篇:python中numpy.zeros(np.zeros)的使用方法

    用法:zeros(shape, dtype=float, order='C') 返回:返回来一个给定形状和类型的用0填充的数组: 参数:shape:形状 dtype:数据类型,可选参数,默认numpy ...

  6. nginx Keepalived高可用集群

    一.Keepalived高可用 1.简介 Keepalived软件起初是专为LvS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此, ...

  7. python3.7 倒计时

    #!/usr/bin/env python __author__ = "lrtao2010" # python3.7 倒计时 import time for i in range( ...

  8. 7款公认比较出色的Python IDE,你值得拥有!

    Python作为一款比较“简洁”的编程语言,它拥有很多性价比高的性能,造就了它现在比较火热的局面,很多人都来学习Python.Python 的学习过程少不了 IDE 或者代码编辑器,或者集成的开发编辑 ...

  9. Redis实现之字典跳跃表

    跳跃表 跳跃表是一种有序数据结构,它通过在每个节点中维持多个指向其他节点的指针,从而达到快速访问节点的目的.跳跃表支持平均O(logN).最坏O(N)的时间复杂度查找,还可以通过顺序性操作来批量处理节 ...

  10. cf982d Shark

    ref #include <algorithm> #include <iostream> #include <cstdio> #include <map> ...