蓝桥云客 找素数
1.找素数 - 蓝桥云课
找素数
题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
素数就是不能再进行等分的整数。比如:7,11。而9不是素数,因为它可以平分为3等份。一般认为最小的素数是2,接着是3,5,...
请问,第100002(十万零二)个素数是多少?
请注意:“2”是第一素数,“3”是第二个素数,依此类推。
运行限制
- 最大运行时间:1s
- 最大运行内存:128M
统计信息
- 总通过次数:3396
- 总提交次数:3598
- 通过率:94.4%
难度
- 中等
- 标签:2012,国赛
版权声明
思路:
暴力
代码:
#include <iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
const ll N = 1e5+10;
ll tot = 1;
ll prime[N];
bool is_prime(ll x)
{
if(x == 2)
return true;
else if(x < 2)
return false;
for(ll i = 2 ; i * i <= x ; i++)
{
if(x % i == 0)
return false;
}
return true;
}
int main()
{
for(ll i = 2 ; tot <= 100002 ; i++)
{
if(is_prime(i))
{
prime[tot] = i;
tot++;
}
}
cout << prime[100002];
return 0;
}
思路:
线性筛
代码: