码迷,mamicode.com
首页 > 其他好文 > 详细

NEFU117 素数个数的位数【素数定理】

时间:2015-02-02 23:09:34      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

题目连接:

http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=117


题目大意:

给你一个整数N(1 < N < 1000000000),如果小于10^N的整数中素数的个数为π(N),

那么问题来了:求π(N)的位数是多少。


思路:

素数的个数π(N)有素数定理可得:π(N) = N/ln(N)。本题中π(10^N) = 10^N/ln(10^N)。

问题就转换为:求N^10*ln(N^10)共有多少位。设共有x位,可得 10^x = 10^N/ln(10^N)。

对两边同时取对数log10,得:

             10^x = N^10 / ln(N^10)

 log10(10^x) = log10( 10^N / ln(10^N)

                    x = N - log10( ln(10^N) )

                    x = N - ( log10(N) + log10( ln10 )

                    x = N - log10(N) - log10( ln10 )

最后x取整+1,得出对应的位数。


素数定理:

对正实数x,定义π(x)为不大于x的素数个数。当x趋近∞,π(x) 和x/ln x的比趋近1。


AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const double e = 2.71828;

int main()
{
    int N;
    while(cin >> N)
    {
        double M = double(N) - log10(N) - log10(log(10));
        cout << (int)M + 1 << endl;
    }

    return 0;
}


NEFU117 素数个数的位数【素数定理】

标签:

原文地址:http://blog.csdn.net/lianai911/article/details/43417327

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!