素数个数的位数
Time Limit 1000ms
Memory Limit 65536K
description
小明是一个聪明的孩子,对数论有着很浓烈的兴趣。他发现求1到正整数10n 之间有多少个素数是一个很难的问题,该问题的难以决定于n 值的大小。现在的问题是,告诉你n的值,让你帮助小明计算小于10n的素数的个数值共有多少位?
input
输入数据有若干组,每组数据包含1个整数n(1 < n < 1000000000),若遇到EOF则处理结束。
output
对应每组数据,将小于10n 的素数的个数值的位数在一行内输出,格式见样本输出。同组数据的输出,其每个尾数之间空一格,行末没有空格。
sample_input
3
7
sample_output
3
6
hint
素数定理
题目很明了,考查素数定理:设p(n)是从1到n之间素数的个数,则随着n的增大,p(n)/(n/ln(n))=1。
要求一个数x的位数,用公式:lg(x)+1; 在本题中 [ lg( n/ ln(n) ) ] +1 即为所求。
而在本题当中的’n’是指10的n次方,即10^n,这个不要弄错了。
lg(x):以10为底,x的对数;
ln(x):为e为底,x的对数。
下面来推算一下:
位数为:lg( (10^n) / ln ( 10^n ) )+1
= lg(10^n) - lg( ln( 10^n) ) +1
= n - lg( n*ln(10) ) +1
= n- lg(n) - lg( ln(10) ) +1
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<cmath>
#include <iomanip>
typedef long long ll;
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
double n,m;
int s;
while(cin>>n)
{
m=(n-log10(n)-log10(log(10)));
s=(int)m+1;
cout<<s<<endl;
}
return 0;
}