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

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 ;
}

最新文章

  1. Thinking in Java——笔记(11)
  2. SQL Server Management Studio 已停止工作 异常错误
  3. POJ 3080 后缀数组/KMP
  4. js兼容方法:通过样式名获取元素,byClass
  5. PHP定时执行任务的实现
  6. php上传文件时出现错误:failed to open stream: Permission denied
  7. for语句之侦查队挑选人、猴子吃桃、5个小朋友算年龄、1 () 2 () 3 ()4 = 4;问括号里我要填 (- 或 +)问题
  8. C++获取文件大小常用技巧
  9. MVC 5 Scaffolding多层架构代码生成向导开源项目
  10. secache 详解
  11. kali linux 2.0下搭建DVWA渗透测试演练平台
  12. Maven 设置Maven源/镜像
  13. 使用Lock锁生产者消费者模式
  14. Linux2.6--Linus电梯
  15. jQuery_base
  16. Python图像处理之验证码识别
  17. retrofit框架接口调用时候报Throwing new exception
  18. js原生语法实现表格操作
  19. BZOJ 1934 善意的投票
  20. Java NIO系列教程(十一) Java NIO 与 IO

热门文章

  1. Python之异常追踪模块:traceback
  2. 多字段 java对象排序
  3. Effective C++ -----条款45:运用成员函数模板接受所有兼容类型
  4. Spring Data JPA初使用(转载)
  5. Mac系统搭建java开发环境
  6. HDU 5995 Kblack loves flag ---BestCoder Round #90
  7. NEFU 505 超级红与黑 (高斯消元)
  8. 20145213《Java程序设计》实验五Java网络编程及安全
  9. jquery阻止事件冒泡的3种方式
  10. eclipse查看hadoop中文件出现乱码