暂时没有时间整理,先放在这里:

http://www.quora.com/Prime-Numbers/What-are-good-ways-to-find-nth-prime-number-in-the-fastest-way

—————————————————————————————————————————————————————————————————————

This is an algorithm to test whether or not a given integer is prime. It's called the AKS primality test http://en.m.wikipedia.org/wiki/A...

And can be done in polynomial time, which is usually considered a decent amount of time.

Now if you're trying to compute the nth prime, it has been proven that the nth prime must be greater than

and less than

When . So if you're searching for the nth prime, I'd look in this gap.

——————————————————————————————————————————————————————————————————————

If it is an interview question, I believe Sieve of Eratosthenes algorithm is a pretty good answer. For not too large n, the algorithm should work fine. For extremely large n, then you probably have to use a precomputed array of bins. Each covers a range of [i*N-i*(N+1)-1] and the number of prime numbers in that range as described in [2].

There is no general formula to compute nth prime number.

[1] http://en.wikipedia.org/wiki/Sie...
[2] http://primes.utm.edu/nthprime/a...

  

———————————————————————————————————————————————————————————————————————

附上一个同学写的用bitarray实现的http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes求素数的方法:

/*
* ========================================================================
*
* Filename: bitset.h
*
* Description: bitset implementation in c.
*
* Created: 05/27/2013 11:09:43 PM
*
* Author: Fu Haiping (forhappy), haipingf@gmail.com
* Company: ICT ( Institute Of Computing Technology, CAS )
*
* ========================================================================
*/
#include <limits.h> /* for CHAR_BIT */ #define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))
#define BITNSLOTS(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT)
/*
* char bitarray[BITNSLOTS(47)];
* BITSET(bitarray, 23);
* if(BITTEST(bitarray, 35)) ...
*
* */ #include <stdio.h>
#include <string.h> #define MAX 10000 int main()
{
char bitarray[BITNSLOTS(MAX)];
int i, j; memset(bitarray, , BITNSLOTS(MAX)); for(i = ; i < MAX; i++) {
if(!BITTEST(bitarray, i)) {
printf("%d\n", i);
for(j = i + i; j < MAX; j += i)
BITSET(bitarray, j);
}
}
return ;
}

计算第K个素数的更多相关文章

  1. pta 习题集 5-14 求n以内最大的k个素数以及它们的和

    本题要求计算并输出不超过n的最大的k个素数以及它们的和. 输入格式: 输入在一行中给出n(10≤≤n≤≤10000)和k(1≤≤k≤≤10)的值. 输出格式: 在一行中按下列格式输出: 素数1+素数2 ...

  2. 求n以内最大的k个素数以及它们的和

    本题要求计算并输出不超过n的最大的k个素数以及它们的和. 输入格式: 输入在一行中给出n(10≤n≤10000)和k(1≤k≤10)的值. 输出格式: 在一行中按下列格式输出: 素数1+素数2+-+素 ...

  3. 求大于整数m且紧靠m的k个素数 及 判断一个数是否为素数的方法

    题目: 请编写一个函数void fun(int m,int k ,int xx[]),该函数的功能是:将大于整数m且紧靠m的k个素数存入xx所指的数组中. 例如,若输入:17,5,则应输出:19,23 ...

  4. 【算法】php计算数字k在一段数字范围出现的次数

    计算数字k在0到n中的出现的次数,k可能是[0~9]内的一个值. 例如数字n=25,k=1,在1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...

  5. 用python计算100以内的素数

    用python计算100以内的素数 : break else: list.append(i)print(list)

  6. BZOJ 3181([Coci2012]BROJ-最小质因子为p的第k小素数)

    3181: [Coci2012]BROJ Time Limit: 10 Sec   Memory Limit: 64 MB Submit: 26   Solved: 7 [ Submit][ Stat ...

  7. 计算第k个质因数只能为3,5,7的数

    英文描述:Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7 思路: ...

  8. 【第k小素数 】 打表问题

    Prime Number TimeLimit: 1 Second MemoryLimit: 32 Megabyte Totalsubmit: 399 Accepted: 88 Description ...

  9. 第k个素数

    题目描述 Output the k-th prime number. 输入描述: k≤10000 输出描述: The k-th prime number. #include <iostream& ...

随机推荐

  1. 不懂点CAP理论,你好意思说你是做分布式的吗?

  2. python to be linux daemon

    所需第三方库:python-daemon[https://pypi.python.org/pypi/python-daemon/] 使用方式: python linux_service.py star ...

  3. jQuery DataTables && Django serializer

    jQuery DataTables https://www.datatables.net 本文参考的官方示例 http://datatables.net/release-datatables/exam ...

  4. adb 服务端口2037被占,导致adb和appium无法工作

    症状1: 命令行运行 adb 相关命令,提示如下: adb server is out of date. killing...ADB server didn't ACK* failed to star ...

  5. 设计算法,求AB两个整数集合的交集

    [本文链接] http://www.cnblogs.com/hellogiser/p/ab-set-intersection.html [分析] 思路1:排序法 对集合A和集合B进行排序(升序,用快排 ...

  6. 用C语言把双向链表中的两个结点交换位置,考虑各种边界问题。

    用C语言把双向链表中的两个结点交换位置,考虑各种边界问题. [参考] http://blog.csdn.net/silangquan/article/details/18051675

  7. cf515d

    题意:给出一个矩阵迷宫,要求用1×2的积木填满空白区域,问解法是否唯一,如果无解或者多解均输出“Not unique". 分析:广搜.看似二分图匹配但实际上不是. 我们认为每个点和上下左右四 ...

  8. Semaphore(信号量)

    Semaphore msdn介绍: 限制可同时访问某一资源或资源池的线程数. 命名空间: System.Threading 程序集: System(在 System.dll 中) 通俗理解: 1:宾馆 ...

  9. Kafka集群环境搭建

    Kafka是一个分布式.可分区.可复制的消息系统.Kafka将消息以topic为单位进行归纳:Kafka发布消息的程序称为producer,也叫生产者:Kafka预订topics并消费消息的程序称为c ...

  10. frxReport 设计 (mtm)

    ► 设计  frxReport  frxReport 窗体上放一个  [frxReport] 的控件 双击 [frxReport]控件 进入设置模式 frxReport1.ShowReport() 方 ...