Harmonic series: Difference between revisions

Content added Content deleted
(add parigp)
(added AWK)
Line 95: Line 95:
Position of the first harmonic number > 9: 4550
Position of the first harmonic number > 9: 4550
Position of the first harmonic number > 10: 12367
Position of the first harmonic number > 10: 12367
</pre>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f HARMONIC_SERIES.AWK
# converted from FreeBASIC
BEGIN {
limit = 20
printf("The first %d harmonic numbers:\n",limit)
for (n=1; n<=limit; n++) {
h += 1/n
printf("%2d %11.8f\n",n,h)
}
print("")
h = 1
n = 2
for (i=2; i<=10; i++) {
while (h < i) {
h += 1/n
n++
}
printf("The first harmonic number > %2d is %11.8f at position %d\n",i,h,n-1)
}
exit(0)
}
</lang>
{{out}}
<pre>
The first 20 harmonic numbers:
1 1.00000000
2 1.50000000
3 1.83333333
4 2.08333333
5 2.28333333
6 2.45000000
7 2.59285714
8 2.71785714
9 2.82896825
10 2.92896825
11 3.01987734
12 3.10321068
13 3.18013376
14 3.25156233
15 3.31822899
16 3.38072899
17 3.43955252
18 3.49510808
19 3.54773966
20 3.59773966

The first harmonic number > 2 is 2.08333333 at position 4
The first harmonic number > 3 is 3.01987734 at position 11
The first harmonic number > 4 is 4.02724520 at position 31
The first harmonic number > 5 is 5.00206827 at position 83
The first harmonic number > 6 is 6.00436671 at position 227
The first harmonic number > 7 is 7.00127410 at position 616
The first harmonic number > 8 is 8.00048557 at position 1674
The first harmonic number > 9 is 9.00020806 at position 4550
The first harmonic number > 10 is 10.00004301 at position 12367
</pre>
</pre>