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. 继承users表,添加新字段成一个新表

    1. Tools > Run manage.py Task 创建app,users startapp users 2.修改users中的models from django.db import ...

  2. windows7 下安装python3.6开发环境

    所有的软件都放在百度云盘里: 链接: https://pan.baidu.com/s/1rux8sDK9thhbZ1qjwQg6kA 密码: iq4c 1. 安装python3.6.5 安装的时候要把 ...

  3. HDU 3345

    http://acm.hdu.edu.cn/showproblem.php?pid=3345 最近重写usaco压力好大,每天写的都想吐.. 水一道bfs 注意的是开始旁边有敌人可以随便走,但是一旦开 ...

  4. sha1的加密

    from hashlib import sha1 #给password加密s1 = sha1() #创建sha1加密对象s1.update(password.encode("utf-8&qu ...

  5. C++ 回调函数的几种策略

    Stackoverflow中提出了这样一个问题:假设我们实现了一个User类,Library类,现在Library类中utility需要回调User中func方法,总结答案,常见的几种方法如下: 静态 ...

  6. IOS在Document目录下创建文件夹、保存、读取、以及删除文件

    // 在Documents目录下创建一个名为LaunchImage的文件夹 NSString *path = [[NSHomeDirectory() stringByAppendingPathComp ...

  7. java sundry tips

    1.关于Arrays 记得binarySearch方法返回的int 类型的数值的含义.    If the array contains multiple elements with the spec ...

  8. C语言共用体union

    union共用体说明: 当一个共用体被声明时, 编译程序自动地产生一个变量, 其长度为联合中最大的变量长度的整数倍. 比如union中有{int x; double x1; char name[10] ...

  9. javascript 小代码

    if(!("a" in window)){ var a =1; } alert(a); //undefined var a = 1,b=function a (x){ x & ...

  10. Jsp邮件找回密码全攻略

    [来源网络  http://www.2cto.com/kf/201502/376374.html] 一般大型网站我们登录的时候,密码忘了都有个功能可以找回密码. 细数下大致的方法: 1.直接把密码发送 ...