欧拉计划 发表于 2023-5-22 04:00:45

题目7:找出第10001个质数

题目7:找出第10001个质数

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 10001st prime number?

题目翻译:

前六个质数是 2, 3, 5, 7, 11 和 13,其中第 6 个质数是 13。

那么第 10001 个质数是多少?


视频讲解:

https://www.bilibili.com/video/BV1Rj411Q7Ar/


思路解析及源码参考(C & Python):

**** Hidden Message *****

zhangjinxuan 发表于 2023-5-22 19:42:29

本帖最后由 zhangjinxuan 于 2023-5-22 19:53 编辑

优化

我们可以使用算法——欧拉筛,直接筛出质数。

这样的做法是 O(N) 的,会更快。

#include <bits/stdc++.h>
using namespace std;

int c, tot;
bool is;

int main() {
        for (int i = 2; tot <= 10001 && i <= 3000000; ++i) {
                if (!is) c[++tot] = i;
                for (int j = 1; j <= tot && c * i <= 3000000; ++j) {
                        is * i] = 1;
                        if (i % c == 0) break;
                }
        }
        printf("%d\n", c);
    return 0;
}


两者对比,欧拉筛做法比暴力判断快了 50 倍!

PetalWill 发表于 2023-5-23 07:17:10

正常循环判断素数并计数,计数10001时print并输出

迷白 发表于 2023-5-27 14:39:59

1

a8634944 发表于 2023-5-29 00:56:21

11111

whovian133 发表于 2023-5-29 21:20:27

4545

auend 发表于 2023-6-2 14:09:05

good object , thanks

siazb8590 发表于 2023-6-15 22:36:47

我来学习学习

YZSNB 发表于 2023-6-16 01:29:25

111

猫熊同学 发表于 2023-6-19 13:42:29

import math

def isPrime(n):
    for i in range(2, math.isqrt(n)+1):
      if n % i == 0:
            return False
    return True

i = 1
n = 0
while True:
    i += 1
    if isPrime(i):
      # print(i)
      n += 1
    if n == 10001:
      print(i)
      break

# 答案是104743

歌者文明清理员 发表于 2023-7-3 07:04:10

def isprime(n):
    for i in range(2, int(n ** 0.5) + 1):
      if n % i == 0:
            return False
    return True

count = 0
i = 2
while count < 10001:
    if isprime(i):
      count += 1
    i += 1
print(i - 1)

pixie99 发表于 2023-7-7 17:54:35

0

傲然子 发表于 2023-7-7 22:22:18

让我来康康优化版的答案

gurite 发表于 2023-7-8 19:34:32

{:5_109:}

不是黄瓜是傻瓜 发表于 2023-7-23 21:50:30

阿斯顿

sfqxx 发表于 2023-7-25 19:14:48

1

liangxixin 发表于 2023-8-1 16:35:01

1

奋斗中的鱼 发表于 2023-8-15 18:16:10

源码

nkysp 发表于 2023-8-18 15:06:12

OK

左右丶 发表于 2023-10-10 16:28:36

我来学习学习
页: [1] 2
查看完整版本: 题目7:找出第10001个质数