Chinaunix首页 | 论坛 | 博客
  • 博客访问: 216731
  • 博文数量: 68
  • 博客积分: 3120
  • 博客等级: 中校
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-08 09:53
文章分类
文章存档

2012年(29)

2011年(3)

2010年(18)

2009年(18)

我的朋友

分类: C/C++

2012-01-18 20:11:21

问题:

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


答案:104743

#include
#include
#include

int isPrime(int n)
{
    if(1 == n) return 0;
    else if(4 > n ) return 1;
    else if(0 == (n%2)) return 0;
    else if(9 > n) return 1; //4, 6, 8 is excluded by row 7;
    else if(0 == (n%3)) return 0;
    else {
        int r = ( int)floor(sqrt((double)n));
        int f = 5;
        while(f <= r){
            if(0 == (n%f)) return 0;
            if(0 == (n%(f+2))) return 0;
            f += 6;
        }
        return 1;
    }
}

int main(void)
{
    const int limit = 10001;
    int count=1;
    int n = 1;
    do{
        n += 2;
        if(isPrime(n)) count++;
    }while(count < limit);
    printf("%d\n", n);
    return 0;
}

/* The answer is 104743 .*/


阅读(473) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~