素数定理

版权声明:转载请注明出处:https://blog.csdn.net/qq1013459920 https://blog.csdn.net/qq1013459920/article/details/83246457

 

Description

小明是一个聪明的孩子,对数论有着很浓烈的兴趣。他发现求1到正整数10n 之间有多少个素数是一个很难的问题,该问题的难以决定于n 值的大小。现在的问题是,告诉你n的值,让你帮助小明计算小于10n的素数的个数值共有多少位?
                            
input

输入数据有若干组,每组数据包含1个整数n(1 < n < 1000000000),若遇到EOF则处理结束。
                            
output

对应每组数据,将小于10n 的素数的个数值的位数在一行内输出,格式见样本输出。同组数据的输出,其每个尾数之间空一格,行末没有空格。
                            
sample_input

3
7

                            
sample_output

3
6

利用素数定理,用n/ln n的位数代替.根据位数公式 ln(n/ln n)+1就是该问题的解

 

解析:将公式化简得到:n=int((double)n-log10((double)n)-log10(log(10.0))+1.0);

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
 
int main()
{
    int n;
    while(scanf("%d",&n) != -1)
    {
        if(n == 1 || n == 2)
        {
            printf("%d\n",n);
            continue;
        }
        n=int((double)n - log10((double)n) - log10(log(10.0)) + 1.0);
        printf("%d\n",n);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/83246457