poj 3518 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的更多相关文章
- POJ 3518 Prime Gap(素数)
POJ 3518 Prime Gap(素数) id=3518">http://poj.org/problem? id=3518 题意: 给你一个数.假设该数是素数就输出0. 否则输出比 ...
- POJ 3518 Prime Gap(素数题)
[题意简述]:输入一个数,假设这个数是素数就输出0,假设不是素数就输出离它近期的两个素数的差值,叫做Prime Gap. [分析]:这题过得非常险.由于我是打的素数表. 由于最大的素数是1299709 ...
- POJ 3581 Prime Gap(二分)
Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11009 Accepted: 6298 Descriptio ...
- 【UVA - 1644 / POJ - 3518】Prime Gap(水题)
Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每 ...
- [暑假集训--数论]poj3518 Prime Gap
The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- sicily 1500. Prime Gap
Description The sequence of n ? 1 consecutive composite numbers (positive integers that are not prim ...
随机推荐
- Android Studio3.0 新特性 ~ New Features in Android Studio Preview (译文)
原文地址:https://developer.android.google.cn/studio/preview/features/index.html 最新Android Studio版本是Andro ...
- 基于Ubuntu16.04的GeForce GTX 1080驱动安装,遇到的问题及对应的解决方法
1.在主机上插上GPU之后,查看设备: $ nvidia-smi Tue Dec :: +------------------------------------------------------- ...
- chrome 55 zepto tap事件出错?
刚才升级chrome后发现的,在54.0.2840.98上点击没有问题,在新升级的55.0.2883.75 上点击后会报错Cannot read property 'trigger' of undef ...
- linux安装配置apache服务(httpd)
1. 安装 httpd. [root@linuxprobe ~]# yum -y install httpd 2. 删除默认欢迎页面 [root@linuxprobe ~]# rm -f /etc/h ...
- learn go function callback
package main // 参考文档: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.7.md im ...
- .NET实现CORS跨域
1.NuGet:Install-Package Microsoft.AspNet.WebApi.Cors 2.Application_Start中增加(包含在#if DEBUG中):GlobalCon ...
- Flask第六篇——项目配置文件
我们在开发中,通常将一些需要用到的配置选项单独放在一个文件中,比如叫configs.py中.然后通过一些方式加载. 现在将加载配置文件的方法罗列如下: 1.先新建文件configs.py,文件代码: ...
- 学习Selenium同学必看
本文转载 作者:灰蓝蓝蓝蓝蓝蓝链接:http://www.jianshu.com/p/5188cb3ab790來源:简书著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.更多技术博客 ...
- 《selenium2 python 自动化测试实战》(3)——操作测试对象
上一节我们说了如何定位元素,定位到元素以后就涉及到对元素的操作了,webdriver中常用的操作元素的方法有: clear ——用于清除输入框的默认内容 send_keys ——用于在一个输入框里 ...
- 【mysql】VS2017 EF使用 mysql
要在vs2017中能打开mysql的连接,需要安装以下扩展 mysql-connector-net-6.9.9+.msi [这里注意不要下载最新8.x.y版本,有坑]mysql-for-visuals ...