【模板题】数论之素数:素数筛法与暴力求解:10:判决素数个数,10:素数对,05:素数回文数的个数,43:质因数分解,11:回文素数:

10:判决素数个数  注意题目没说x<y呀!!!

1、素数筛法:即获得一个素数,则标记其倍数为非素数。所有没被之前的数标记的则为素数。注意初值为2*i

#include<iostream>
#include<string.h>
#include<algorithm>
#define MAX 100001
using namespace std;
bool prime[MAX];
int main()
{
	int i,j,x,y,ans=0;
	memset(prime,1,sizeof(prime));
	prime[1]=0;prime[0]=0;
	for (i=2;i<=MAX;i++)
	{
		if (prime[i])
		{
			for (j=i*2;j<MAX;j+=i)//注意初值为2*i
				prime[j]=false;
		}
	}
	cin>>x>>y;
	if (x>y)//坑啊********************
		swap(x,y);
	for (i=x;i<=y;i++)
		if (prime[i])
			ans++;
	cout<<ans;
	return 0;
}

2、暴力循环 1~sqrt(i)

#include<iostream>
#include<string.h>
#include<algorithm>
#define MAX 100001
using namespace std;
bool prime[MAX];
int main()
{
	int i,j,x,y,ans=0,n;
	memset(prime,1,sizeof(prime));
	prime[0]=0;prime[1]=0;
	for (i=2;i<=MAX;i++)
	{
		n=sqrt(i);
		for (j=2;j<=n;j++)
			if ((i%j)==0)
			{
				prime[i]=false;
				break;
			}
	}
	cin>>x>>y;
	if (x>y)//坑啊********************
		swap(x,y);
	for (i=x;i<=y;i++)
		if (prime[i])
			ans++;
	cout<<ans;
	return 0;
}

练习1:10:素数对

两个相差为2的素数称为素数对,如5和7,17和19等,要求找出所有两个数均不大于n的素数对

#include<iostream>
#include<string.h>
#define MAX 100001
using namespace std;
bool prime[MAX];
int main()
{
	bool flag=false;
	int i,j,n;
	memset(prime,1,sizeof(prime));
	prime[1]=0;
	for (i=2;i<MAX;i++)
	{
		if (prime[i])
			for (j=i*2;j<MAX;j+=i)
				prime[j]=0;
	}
	cin>>n;
	for (i=1;i<=n-2;i++)
		if (prime[i] && prime[i+2])
		{
			flag=true;
			cout<<i<<" "<<i+2<<endl;
		}
	if(!flag)
		cout<<"empty"<<endl;
	return 0;
}

练习2:05:素数回文数的个数:求11到n之间(包括n),既是素数又是回文数的整数有多少个。

#include<iostream>
#include<string.h>
#define MAX 1001
using namespace std;
bool prime[MAX];
bool judge(int x)
{
	int a[4],top=0,i,j;
	while(x)
	{
		a[top++]=x%10;
		x=x/10;
	}
	i=0;j=top-1;
	while(i<=j)
	{
		if (a[i]!=a[j])
			return 0;
		i++;j--;
	}
	return 1;
}
int main()
{
	int i,j,n,ans=0;
	memset(prime,1,sizeof(prime));
	prime[1]=false;
	for (i=2;i<=MAX;i++)
		if (prime[i])
		{
			for (j=i*2;j<MAX;j+=i)
				prime[j]=false;
		}
	cin>>n;
	for (i=11;i<=n;i++)
		if (prime[i]&& judge(i))
			ans++;
	cout<<ans<<endl;
	return 0;
}

练习3:43:质因数分解:已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数。

#include<iostream>
#include<algorithm>
using namespace std;
bool judge(int x)
{
	int i,n=sqrt(x);
	for (i=2;i<=n;i++)
		if (x%i==0)
			return false;
	return true;
}
int main()
{
	int a,i;
	cin>>a;
	int x=sqrt(a);
	for (i=1;i<=x;i++)
		if (a%i==0 && judge(i) && judge(a/i) )
			break;
	cout<<a/i;
	return 0;
}

练习4:11:回文素数:这道题要先生成回文再判断素数

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int n;
vector<int>ans;
bool isprime(int a)
{
	int x=sqrt(a),i;
	for (i=2;i<=x;i++)
		if (a%i==0)
			return false;
	return true;
}
int trans(int x)
{
	int a[10],top=0,ans=0,i;
	while(x>0)
	{
		a[top++]=x%10;x=x/10;
	}
	if (n%2==0)
	{
		for (i=top-1;i>=0;i--)
			ans=ans*10+a[i];
		for (i=0;i<top;i++)
			ans=ans*10+a[i];
	}
	else 
	{
		for (i=top-1;i>=0;i--)
			ans=ans*10+a[i];
		for (i=1;i<top;i++)
			ans=ans*10+a[i];
	}
	return ans;
}
int main()
{
	int i,s=1,t=1,a,count=0,temp;
	cin>>n;
	if (n==1){cout<<4<<endl<<"2 3 5 7"<<endl;return 0;}
	temp=(n+1)/2;
	while(temp--) t=t*10;
	s=t/10;
	for (i=s;i<t;i++)
	{
		a=trans(i);
		if (isprime(a))
		{
			count++;
			ans.push_back(a);
		}
	}
	cout<<count<<endl;
	for (i=0;i<ans.size();i++)
		cout<<ans[i]<<" ";
	return 0;
}


猜你喜欢

转载自blog.csdn.net/always_ease/article/details/80683946
今日推荐