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

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. 向SqlServer数据库插入数据

    Insert Values Insert Select Insert Exec Select Into Bulk Insert Insert Values是最常用的一种插入数据的方式,基本语法如下,表 ...

  2. 【leetcode】Search in Rotated Sorted Array

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  3. poj 1182

    http://poj.org/problem?id=1182 一个利用并查集的经典题目. 思路:在网上看到别人的思路,觉得方法还是挺不错的. 首先,开辟一个3*n的数组belg,用来存b和c的关系,在 ...

  4. oracle11g 连接问题

    一.The Network Adapter could not establish the connection  状态: 失败 -测试失败: IO 错误: The Network Adapter c ...

  5. 解读Unity中的CG编写Shader系列五(理论知识)

    转自 http://www.itnose.net/detail/6098474.html 经过前面的系列文章中的三个例子,尽管代码简单,但是我想应该还有些地方没有100%弄明白,我们现在得回过头来补充 ...

  6. ios Tabbar Item 的图标

    1,tabBarItem图片的推荐尺寸和最大支持尺寸 下面是标签栏(UITabBar)中tab按钮图标分别在1x.2x.3x下不会压缩变形的尺寸: @1x : 推荐 25 x 25   (最大: 48 ...

  7. 游戏服java程序启动,显示内存溢出

    1.OutOfMemoryError:Java heap space 过程:服务器上面的mysql突然异常重启,导致了程序启动的时候报错 问题1:OutOfMemoryError:Java heap ...

  8. cxGrid 增加序号 (非数据库绑定模式) (测试通过)

    cxGrid 增加序号 (非数据库绑定模式) ----------------------------------- 1. 选在 adoQuery 控件 , 鼠标右键菜单中 选择 Fields Edi ...

  9. MySQL(MariaDB)的 SSL 加密复制

    背景: 在默认的主从复制过程或远程连接到MySQL/MariaDB所有的链接通信中的数据都是明文的,在局域网内连接倒问题不大:要是在外网里访问数据或则复制,则安全隐患会被放大很多.由于项目要求需要直接 ...

  10. winrt 真正的绑定任意命令

    下载codeplex上的winrttrigger开源控件 Winrt.Tirgger.EventTrigger trigger = new Winrt.Tirgger.EventTrigger();  ...