Prime Gap(素数表+二分)

本文介绍了一个程序设计任务:计算特定正整数所在素数间隙的长度。通过预先生成素数表,并采用二分查找法确定最接近指定正整数的素数位置,进而计算出素数间隙的长度。
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

//首先打印素数表,然后用二分查找最接近m且大于m的素数在素数表中的下标,然后求区间长度(即该素数减去上一个素数的值);

#include<stdio.h>
#include<string.h>
#define Max 1299709+4
#define Len 100004
int prime[Len],tot;
int vis[Max];
struct node
{
	int left,right,gap;
}tree[4*Len];
void make_prime()
{
	memset(vis,0,sizeof(vis));
	tot=1;
	int i,j;
	for(i=2;i<Max;i++)
	{
		if(!vis[i])
		{
			prime[tot++]=i;
			for(j=i+i;j<Max;j+=i)
				vis[j]=1;
		}
	}
}
int find(int low,int high,int m)
{
	int mid;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(m<=prime[mid])
			high=mid-1;
		else low=mid+1;
	}
	return low;
}
int main()
{
	//freopen("b.txt","r",stdin);
	int m;
	make_prime();
	while(scanf("%d",&m)==1,m)
	{
		int tmp=find(1,tot-1,m);
		if(prime[tmp]==m) puts("0");
		else
		printf("%d\n",prime[tmp]-prime[tmp-1]);
	}
	return 0;
}



在提供的代码中,`prime[cnt++]` 出现在欧拉筛法的实现里。以下是对其含义、作用及使用场景的详细分析。 ### 含义 `prime[cnt++]` 这一表达式结合了数组访问和自增操作。具体而言,`prime` 是一个数组,`cnt` 是一个整数变量。`prime[cnt++]` 等价于先访问 `prime[cnt]`,接着将 `cnt` 的值增加 1。其具体步骤为: 1. 访问 `prime` 数组中索引为 `cnt` 的元素。 2. 使用这个元素进行相应操作。 3. 把 `cnt` 的值加 1。 ### 作用 在欧拉筛法的代码中,`prime[cnt++]` 的作用是将找到的素数存储到 `prime` 数组里,同时更新素数的计数。具体代码如下: ```cpp #include <bits/stdc++.h> using namespace std; const int maxn=1e6+5; int prime[maxn]; bool st[maxn]; int euler_sieve(int n) { int cnt=0; memset(prime,0,sizeof(prime)); memset(st,0,sizeof(st)); for(int i=2; i<=n; i++) { if(!st[i]) prime[cnt++]=i; // 将素数 i 存储到 prime 数组中,并更新计数 cnt for(int j=0; j<cnt && i*prime[j]<=n; j++) { st[i*prime[j]]=true; if(i%prime[j]==0) break; } } return cnt; } ``` 在这段代码中,当 `!st[i]` 为真时,意味着 `i` 是素数,此时将 `i` 存储到 `prime[cnt]` 位置,随后 `cnt` 自增 1,这样就能把下一个找到的素数存到下一个数组位置。 ### 使用场景 `prime[cnt++]` 主要用于需要动态存储数据,并且同时统计数据数量的场景。在欧拉筛法里,就是动态存储找到的素数,同时统计素数的个数。 ### 示例代码 ```cpp #include <iostream> #include <cstring> using namespace std; const int maxn = 100; int prime[maxn]; bool st[maxn]; int euler_sieve(int n) { int cnt = 0; memset(prime, 0, sizeof(prime)); memset(st, 0, sizeof(st)); for (int i = 2; i <= n; i++) { if (!st[i]) prime[cnt++] = i; for (int j = 0; j < cnt && i * prime[j] <= n; j++) { st[i * prime[j]] = true; if (i % prime[j] == 0) break; } } return cnt; } int main() { int n = 20; int prime_count = euler_sieve(n); cout << "Prime numbers up to " << n << ": "; for (int i = 0; i < prime_count; i++) { cout << prime[i] << " "; } cout << endl; return 0; } ``` 在这个示例中,`prime[cnt++]` 把找到的素数存储到 `prime` 数组,最后输出这些素数。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值