7.10001st prime

问题描述

By listing the first six prime numbers:$2,3, 5, 7, 11$,and $13$, we can see that the $6th$ prime is $13$.

What is the $10 001$st prime number?

解法

我采用的是从第一个素数开始判断,如果是素数的话,就对$cnt$ 进行$+1$操作,直到$cnt == 100001$的时候为止。写一个是否为素数的判断函数即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
#define ll long long
#define IOS {ios::sync_with_stdio(false);cin.tie(0);}
using namespace std;

const int maxn = 10001;
int i, cnt;
bool isprime(int x)
{
if(x==1)
return false;
for(int i=2;i<=sqrt(x);i++)
{
if(x%i==0)
return false;
}
return true;
}
int main(void)
{
IOS
while(cnt < maxn)
{
i++;
if(isprime(i))
cnt++;
}
cout << i << endl;
return 0;
}

同样我们可以采用一种筛法来构建一个素数表,主要是空间要开多少,这里有一个大概的公式:

用$π(x)$表示不超过x的素数个数,当x足够大时,存在

具体见百科(虽然我觉得没什么用…)

https://baike.baidu.com/item/%E8%B4%A8%E6%95%B0/263515?fr=aladdin#5(https://baike.baidu.com/item/%E8%B4%A8%E6%95%B0/263515?fr=aladdin#5)

https://tieba.baidu.com/p/5247968036?red_tag=3211065356

https://blog.csdn.net/qq_40513792/article/details/95597027

注意点

正确答案

官方解答