目录
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
}
2.100以内的素数
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <sys/time.h>
using namespace std;
typedef long long ll;
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 prime[101],pnum=0;
bool p[101] = {0};
void find_prime(){
for(int i=1;i<101;i++){
if(isprime(i)==true){
prime[pnum++] = i;
// p[i] = true;
}
}
}
int main(){
find_prime();
for(int i=0;i<pnum;i++){
printf("%d ",prime[i]);
}
return 0;
}
3.素数筛选
const int maxn = 101;
int prime[maxn],pnum=0;
bool p[maxn] = {0};
void find_prime(){
for(int i=2;i<maxn;i++){
if(p[i]==false){
prime[pnum++] = i;
for(int j=i+1;j<maxn;j+=1){
p[j] = true;
}
}
}
}
4.数素数
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <sys/time.h>
using namespace std;
typedef long long ll;
const int maxn = 1000001;
int prime[maxn],num=0;
bool p[maxn]={0};
void fine_prime(int n){
for(int i=2;i<maxn;i++){
if(p[i]==false){
prime[num++] = i;
if(num==n) break;
for(int j=i+i;j<maxn;j+=i){
p[j] = true;
}
}
}
}
int main(){
int m,n,count=0;
scanf("%d %d",&m,&n);
fine_prime(n);
for(int i=m-1;i<n;i++){
printf("%d",prime[i]);
count++;
if(count%10==0&&count!=0){
printf("\n");
}else{
printf(" ",prime[i]);
}
}
return 0;
}
5.素数对猜想
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <sys/time.h>
using namespace std;
typedef long long ll;
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=1;i+2<=n;i+=2){
if(isprime(i)==true&&isprime(i+2)==true){
count++;
}
}
printf("%d\n",count);
return 0;
}
6.reversible primes
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <sys/time.h>
using namespace std;
typedef long long ll;
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!=0);
for(int i=0;i<len;i++){
n = n*radix+d[i];
}
if(isprime(n)==true) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
7.Hashing
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <sys/time.h>
using namespace std;
typedef long long ll;
const int N = 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[N] = {0};
int main(){
int size,num;
scanf("%d %d",&size,&num);
while(isprime(size)==false){
size++;
}
int value;
for(int i=0;i<num;i++){
scanf("%d",&value);
int pos=value%size;
if(hashtable[pos]==false){
hashtable[pos] = true;
if(i==0) printf("%d",pos);
else printf(" %d",pos);
}else{
int step;
for(step=1;step<size;step++){
pos = (value+step*step)%size;
if(hashtable[pos]==false){
hashtable[pos] = true;
if(i==0) printf("%d",pos);
else printf(" %d",pos);
break;
}
}
if(step==size){
if(i>0) printf(" ");
printf("-");
}
}
}
return 0;
}