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

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. oracle数据库高级应用之《自动生成指定表的insert,update,delete语句》

    /* * 多条记录连接成一条 * tableName 表名 * type 类型:可以是insert/update/select之一 */ create or replace function my_c ...

  2. STL 阅读(浅析)

    写的不错,决定那这个看下.看的还是晕. http://luohongcheng.github.io/archives/

  3. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  4. 【leetcode】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  5. poj 1010

    http://poj.org/problem?id=1010 题意:给你n种邮票的价值,到0结束,这些邮票价值有可能相同,但是形状是不同的. 还有给你m个收藏家所需要收藏的邮票的总价格.到0结束. 每 ...

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

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

  7. 1. javacript高级程序设计-JavaScript简介

    JavaScript诞生于1995年,由Netscape公司布兰登·艾奇开发,JavaScript主要包括三个部分: (1). ECMAScript,由ECMA-262定义,提高核心语言功能 (2). ...

  8. Python 之 【re模块的正则表达式学习】

    摘要: re模块包括操作正则表达式的函数,一些工作中都需要用到,现在说明下使用方法. 使用说明: 一,re模块下的函数:            函数             描述 compile(pa ...

  9. Java for LeetCode 206 Reverse Linked List

    Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...

  10. 基于bshare分享平台,在一个页面上实现多个不同内容的web分享

    <!--引入bshare SDK--><script type="text/javascript" charset="utf-8" src=& ...