Light OJ 1234 Harmonic Number

来源:互联网 发布:最好的听书软件 编辑:程序博客网 时间:2024/05/16 02:13
Harmonic Number
   PDF (English)StatisticsForum
Time Limit: 3 second(s)Memory Limit: 32 MB

In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:

In this problem, you are given n, you have to find Hn.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 108).

Output

For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.

Sample Input

Output for Sample Input

12

1

2

3

4

5

6

7

8

9

90000000

99999999

100000000

Case 1: 1

Case 2: 1.5

Case 3: 1.8333333333

Case 4: 2.0833333333

Case 5: 2.2833333333

Case 6: 2.450

Case 7: 2.5928571429

Case 8: 2.7178571429

Case 9: 2.8289682540

Case 10: 18.8925358988

Case 11: 18.9978964039

Case 12: 18.9978964139

 

 这一题是调和级数,所谓的调和级数就是

形如1/1+1/2+1/3+…+1/n+…的级数称为<a target=_blank target="_blank" class="inner-link decor-none" href="http://www.baidu.com/s?wd=%E8%B0%83%E5%92%8C%E7%BA%A7%E6%95%B0&hl_tag=textlink&tn=SE_hldp01350_v6v6zkg6" rel="nofollow" style="text-decoration: none; color: rgb(45, 100, 179);">调和级数</a>,它是 p=1 的<a target=_blank target="_blank" class="inner-link decor-none" href="http://www.baidu.com/s?wd=p%E7%BA%A7%E6%95%B0&hl_tag=textlink&tn=SE_hldp01350_v6v6zkg6" rel="nofollow" style="text-decoration: none; color: rgb(45, 100, 179);">p级数</a>。 <a target=_blank target="_blank" class="inner-link decor-none" href="http://www.baidu.com/s?wd=%E8%B0%83%E5%92%8C%E7%BA%A7%E6%95%B0&hl_tag=textlink&tn=SE_hldp01350_v6v6zkg6" rel="nofollow" style="text-decoration: none; color: rgb(45, 100, 179);">调和级数</a>是发散级数。在n趋于无穷时其部分和没有极限(或部分和为无穷大)。1 +1/2+1/3 +1/4 + 1/5+ 1/6+1/7+1/8 +...   1/2+1/2+(1/4+1/4)+(1/8+1/8+1/8+1/8)+...   注意后一个级数每一项对应的分数都小于<a target=_blank target="_blank" class="inner-link decor-none" href="http://www.baidu.com/s?wd=%E8%B0%83%E5%92%8C%E7%BA%A7%E6%95%B0&hl_tag=textlink&tn=SE_hldp01350_v6v6zkg6" rel="nofollow" style="text-decoration: none; color: rgb(45, 100, 179);">调和级数</a>中每一项,而且后面级数的括号中的数值和都为1/2,这样的1/2有无穷多个,所以后一个级数是趋向无穷大的,进而调和级数也是发散的。 从更广泛的意义上讲,如果An是不全部为0的等差数列,则1/An就称为<a target=_blank target="_blank" class="inner-link decor-none" href="http://www.baidu.com/s?wd=%E8%B0%83%E5%92%8C%E6%95%B0%E5%88%97&hl_tag=textlink&tn=SE_hldp01350_v6v6zkg6" rel="nofollow" style="text-decoration: none; color: rgb(45, 100, 179);">调和数列</a>,求和所得即为调和级数,易得,所有调和级数都是发散于无穷的。

       欧拉解答过程     
1+1/2+1/3+1/4+...+1/n= ln(n+1)+r(r为常量)
他的证明是这样的:
根据Newton的幂级数有:
ln(1+1/x) = 1/x - 1/2x^2 + 1/3x^3 - ...
于是:
1/x = ln((x+1)/x) + 1/2x^2 - 1/3x^3 + ...
代入x=1,2,...,n,就给出:
1/1 = ln(2) + 1/2 - 1/3 + 1/4 -1/5 + ...
1/2 = ln(3/2) + 1/2*4 - 1/3*8 + 1/4*16 - ...
......
1/n = ln((n+1)/n) + 1/2n^2 - 1/3n^3 + ...
相加,就得到:
1+1/2+1/3+1/4+...1/n = ln(n+1) + 1/2*(1+1/4+1/9+...+1/n^2) - 1/3*(1+1/8+1/27+...+1/n^3) + ......
后面那一串和都是收敛的,我们可以定义
1+1/2+1/3+1/4+...1/n = ln(n+1) + r
Euler近似地计算了r的值,约为0.5772156649。这个数字就是后来称作的欧拉常数。不过遗憾的是,我们对这个常量还知之甚少,连这个数是有理数还是无理数都还是个谜。
但是此题中所用是ln(n+0.5)+r;
有的版本是lnn+r;
#include <stdio.h>#include <math.h>#include <string.h>const int ma=1000000;double s[ma+1];double x=0.57721566490153286060651209,ans;int T,n,m;int main(){       for(int i=1;i<1000000;i++)        s[i]=1.0/i*1.0+s[i-1];      while(scanf("%d",&T)==1)      {          int cases=1;          while(T--)          {              scanf("%d",&n);              if(n<=1000000)//基本精度就确定了,因为在它是1的时是0.9826807730题意所说是1.0000000000;题目数据时有误的                printf("Case %d: %.10lf\n",cases,s[n]);              else              {              ans=log(1.0*n+0.5)+x;//公式              printf("Case %d: %.10lf\n",cases,ans);              }              cases++;          }      }}



0 0