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. (转)MapReduce Design Patterns(chapter 4 (part 1))(七)

    Chapter 4. Data Organization Patterns 与前面章节的过滤器相比,本章是关于数据重组.个别记录的价值通常靠分区,分片,排序成倍增加.特别是在分布式系统中,因为这能提高 ...

  2. [置顶] 云端TensorFlow读取数据IO的高效方式

    低效的IO方式 最近通过观察PAI平台上TensoFlow用户的运行情况,发现大家在数据IO这方面还是有比较大的困惑,主要是因为很多同学没有很好的理解本地执行TensorFlow代码和分布式云端执行T ...

  3. [Linux] 随机切分文件内容

    1.从原文件中随机选出若干行 可以直接用shuf命令就可以完成: $ shuf -n source.txt > target.txt shuf命令的说明: $ shuf --help Usage ...

  4. MFMailComposeViewController发送邮件

    1.iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面. 2.项目中需要添加MessageUi.framework.头文件加入 ...

  5. Hello Pythoner!

    首先,欢迎你来到pyer的博客,希望你能有所收获! 然后,pyer之前学C#(原博客地址:初行-博客园),后来转的Python,目前从事服务端研发工作. 最后,相逢便是缘,如果看过pyer的博客后有什 ...

  6. Jmeter-JDBC Connection Configuration和JDBC Request注释

    1.JDBC Connection Configuration Variable Name--变量名,唯一值不可重复,给JDBC Request确定使用哪个配置 Max Number of Conne ...

  7. 向requestAnimationFrame的回调函数中传递参数

    其实跟setTimeout类似,我们知道传参传的是一个函数,那么我们是不是可以用一个匿名函数来包裹这个函数的执行呢function fn(fc){ console.log('fc:',fc) fc++ ...

  8. linux自学(三)之开启虚拟机

    上一篇:linux自学(二)之centos7镜像安装 前面都已经铺垫好了,下面就是开始安装镜像了,真正的开启一台linux机器. 点击开启此虚拟机,如图: 之后就是等待了,然后就是开始配置,首先如下图 ...

  9. python3 scrapy main运行调式

    from scrapy.cmdline import execute import sys import os sys.path.append(os.path.dirname(os.path.absp ...

  10. 字符编码:ASCII,Unicode和UTF-8

    字符编码是计算机技术的基石,想要熟练使用计算机,就必须懂得一点字符编码的知识. 1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两 ...