素数(PAT B1007、PATB 1013、PATA 1015、PATA 1078)

素数又称质数,是指除了1和本身之外,不能被其他数整除的一类数。即对给定的任意的正整数a(1 < a < n),都有n % a != 0成立;否则,如果存在a(1 < a < n),使得n % a == 0,那么称n为合数。应特别注意的是,1既不是素数,也不是合数

素数的判定

bool isPrime(int n){
	if (n <= 1)return false;
	int sqr = (int)sqrt(1.0*n);
	for(int i = 2;i <= sqr; i++){
		if (n % i == 0) return false;
	}
	return true;
}

素数表的获得

const int maxn = 110;
int prime[maxn],pNum = 0;
bool p[maxn] = {false};
void Find_Prime(){
	for(int i = 1; i < maxn;i++){
		if (isPrime(i) == true){
			prime[pNum++] = i;
			p[i] = true;
		}
	}	
}

埃氏筛法

埃氏筛法是众多筛法中最简单且最容易理解的一种。其关键在于一个“筛”子。算法从小到大枚举所有数,对每一个素数,筛去它的所有倍数,剩下的就都是素数了。

const int maxn = 110;
int prime[maxn],pNum = 0;
bool p[maxn] = {false};
void Find_Prime(){
	for(int i = 2; i < maxn;i++){
		if (p[i] == false){
			prime[pNum++] = i;
			for(int j = i+i; j < maxn; j += i){
				p[j] = true;
			}
		}
	}
}

例题1

PAT B 1033 数素数

令 PI 表示第 i 个素数。现任给两个正整数 M≤N≤104 ,请输出 PM到 PN的所有素数。

输入格式:

输入在一行中给出 M 和 N,其间以空格分隔。

输出格式:

输出从 PM到 PN的所有素数,每 10 个数字占 1 行,其间以空格分隔,但行末不得有多余空格。

输入样例:

5 27

输出样例:

11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103

思路:打表即可
104素数,将maxn设大一点

#include<cstdio>

const int maxn = 1000005;
int prime[maxn],pNum = 0;
bool p[maxn] = {false};

void Find_Prime(){
	for(int i = 2; i < maxn;i++){
		if (p[i] == false){
			prime[pNum++] = i;
			for(int j = i+i;j < maxn;j += i){
				p[j] = true;
			}
		}
	}
}

int main(){
	int n,m;
	scanf("%d %d",&n,&m);
	int count = 0;
	Find_Prime();
	for (int i = n-1; i < m; ++i)
	{
		if (count == 10){printf("\n");count = 0;}
		printf("%d", prime[i]);
		count++;
		if (i != m -1 && count != 10)printf(" ");
	}
	return 0;
}

例题2

PAT B 1007 素数对猜想

让我们定义d​n为:dn=pn+1 −pn,其中pI是第i个素数。显然有d1=1,且对于n>1有dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N(<105),请计算不超过N的满足猜想的素数对的个数。

输入格式:

输入在一行给出正整数N。

输出格式:

在一行中输出不超过N的满足猜想的素数对的个数。

输入样例:

20

输出样例:

4

思路:
很容易知道,素数对一定是两个奇数。因此可以在1~n的范围内枚举奇数p,如果p和p+2都是素数,那么令计数器count++;
在for循环中 i+=2

#include<cstdio>
#include<cmath>
bool isPrime(int n){
	if (n <= 1)return false;
	int sqr = (int)sqrt(1.0*n);
	for(int i = 2; i <= sqr; i++ ){
		if (n % i == 0) return false; 
	}
	return true;
}

int main(){
	int n,count = 0;
	scanf("%d",&n);
	for(int i = 3; i + 2 <= n; i+= 2){
		if (isPrime(i) == true && isPrime(i+2) == true){
			count++;
		}
	}
	printf("%d\n", count);
	return 0;
}

例题3

PAT A1015 Reversible Primes(20)

A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.
Now given any two positive integers N (<105) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No

思路:
先判读N是否是素数,如果是,则将N转换为radix进制,保存在数组中,然后再采用“逆序遍历”的方式重新转化为十进制。如果转换后的数为素数输出yes,否则no

代码

#include<cstdio>
#include<cmath>
bool isPrime(int n){
	if (n <= 1)return false;
	int sqr = (int)sqrt(1.0*n);
	for(int i = 2; i <= sqr; i++){
		if (n % i == 0)return false;
	}
	return true;
}

int d[111];
int main(){
	int n,radix;
	while(scanf("%d",&n) != EOF){
		if (n < 0)break;
		scanf("%d",&radix);
		if (isPrime(n) == false){
			printf("No\n");
		}else{
			int len = 0;
			do{
				d[len++] = n%radix;
				n /= radix;
			}while(n);
			for(int i = 0; i < len;i++){
				n = n*radix + d[i];
			}
			if (isPrime(n)){
				printf("Yes\n");
			}else{
				printf("No\n");
			}
		}
	}
	return 0;
}

例题4

PAT A1078 Hashing(25)

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤10
​4
​​ ) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print “-” instead.

Sample Input:

4 4
10 6 4 15

Sample Output:

0 1 4 -

思路:
1、Quadratic probing 是指二次方探测法,当H(a)发生冲突时,让a按a + 12,a - 12, a + 22,a - 22的顺序调整a的值。本题中已经说明只要往正向解决冲突,因此需要按a+12,a+2,…的顺序调整a的值
2、如果step从0~TSize-1进行枚举却仍然无法找到位置,那么对step大于等于Tsize来说也不可能找到位置
在此证明:
设 0 <= x <= TSize,那么
(a + (TSize + x)(TSize +x))%Tsize = (a + Tszie * Tsize + 2Tsize * x + x * x). %Tsize = (a + x * x)%Tsize + TsizeTsize%Tsize + 2 * Tsize * x % Size = (a + x * x) % Tsize = (a +x * x) % Tsize
如果Tsize不是素数的话,先找到大于TSize的最小素数,创建一个 bool hashtable[] 用来记录该地址是否使用,判断 如果使用 (就使用二次方探测法,step 在1-Tsize-1内未找到的话,输出-
否则输出该地址)如果未使用,输出该地址。当然hashtable[i]要改为true。
代码:

#include<cstdio>
#include<cmath>
const int MAXN = 11111;
bool isPrime(int n){
	if( n <= 1 )return false;
	int sqr = (int)sqrt(1.0*n);
	for(int i = 2; i <= sqr; i++){
		if ( n % i == 0)return false;
	}
	return true;
}

bool hashTable[MAXN] = {0};

int main(){
	int n,TSize,a;
	scanf("%d %d",&TSize,&n);
	while(isPrime(TSize) == false){
		TSize++;
	}
	for(int i = 0; i < n;i++){
		scanf("%d",&a);
		int m = a %TSize;
		if (hashTable[m] == false){
			hashTable[m] = true;
			if (i == 0)
				printf("%d",m );
			else printf(" %d",m );
		}else{
			int step;
			for(step = 1; step < TSize; step++){
				m = (a +step*step)% TSize;
				if (hashTable[m] ==false){
					hashTable[m] = true;
					if (i == 0)printf("%d", m);
					else printf(" %d",m);
					break;
				}
			}
			if (step == TSize){
				if (i > 0)printf(" ");
				printf("-");
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值