#include <iostream>
using namespace std;
#include <stdio.h>
bool pri(int N);
int main()
{
int X;
cin>>X;
int i = 1;
int judge = 1;
while(1)
{
if (pri(X / i) == false) {
judge = 0;
break;
}
i *= 10;
if (X <= i) break;
}
if (judge == 1)
cout<<X<<"是幸运素数"<<endl;
else
cout<<X<<"不是幸运素数"<<endl;
return 0;
}
bool pri(int N)
{
if (N == 1) return true;
int count = 0;
for (int j = 2; j < N; ++j)
{
if (N % j == 0)
{
count += 1;
break;
}
}
if (count == 0)
return true;
return false;
}
枚举循环,详见代码
关于判断素数那里,因为一个数如果不是2但是偶数,就一定不是素数,这样枚举会快一点
有用请采纳哈
//幸运素数 2686 22-03-30
#include <bits/stdc++.h>
using namespace std;
bool prime(int n)
{
if (n<2) return 0;
else if (n%2==0 && n!=2) return 0;
else
for (int i=3;i<=sqrt(n);i+=2)
if (n%i==0) return 0;
return 1;
}
bool fortune(int n)
{
bool f=0;
while (n>0)
{
if (prime(n)) n/=10;
else
{
f=1;break;
}
}
if (f==1) return 0;
else return 1;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int m,n;
cin>>m>>n;
for (int i=m;i<=n;i++)
if (fortune(i)) cout<<i<<endl;
return 0;
}