素数定理:随着x的增长,π(x)/(x/ln(x))=1,(π(x)为小于x的素数的个数)。
素数个数的位数 |
||
|
||
description |
||
小明是一个聪明的孩子,对数论有着很浓烈的兴趣。他发现求1到正整数10n 之间有多少个素数是一个很难的问题,该问题的难以决定于n 值的大小。现在的问题是,告诉你n的值,让你帮助小明计算小于10n的素数的个数值共有多少位?
|
||
input |
||
输入数据有若干组,每组数据包含1个整数n(1 < n < 1000000000),若遇到EOF则处理结束。
|
||
output |
||
对应每组数据,将小于10n 的素数的个数值的位数在一行内输出,格式见样本输出。同组数据的输出,其每个尾数之间空一格,行末没有空格。
|
||
sample_input |
||
3
7
|
||
sample_output |
||
3
6
|
解析:依据素数定理,将公式化简得到: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; }
原文:http://blog.csdn.net/knight_kaka/article/details/20286975