Harmonic series: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 31:
 
<br>
 
=={{header|ALGOL 68}}==
Using standard lenghth REAL numbers this can find the first Harmonic number > 10, would probably need higher precision to find Harmonic numbers with larger values.
<lang algol68>BEGIN # find some harmonic numbers, Hn is the sum if the reciprocals of 1..n #
# returns the first n Harmonic numbers #
OP HARMONIC = ( INT n )[]REAL:
BEGIN
[ 1 : n ]REAL h;
h[ 1 ] := 1;
FOR i FROM 2 TO n DO
h[ i ] := h[ i - 1 ] + ( 1 / i )
OD;
h
END # HARMONIC # ;
# find the first 20 000 harmonic numbers #
[]REAL h = HARMONIC 20 000;
# show the first 20 harmonic numbers #
FOR i TO 20 DO
print( ( whole( i, -2 ), ":", fixed( h[ i ], -14, 8 ), newline ) )
OD;
# find the positions of the first harmonic number > n where n in 1... #
INT rqd int := 1;
REAL rqd real := 1;
FOR i TO UPB h DO
IF h[ i ] > rqd real THEN
# found the first harmonic number greater than rqd real #
print( ( "Position of the first harmonic number > ", whole( rqd int, -2 ), ": ", whole( i, 0 ), newline ) );
rqd int +:= 1;
rqd real +:= 1
FI
OD
END</lang>
{{out}}
<pre>
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
Position of the first harmonic number > 1: 2
Position of the first harmonic number > 2: 4
Position of the first harmonic number > 3: 11
Position of the first harmonic number > 4: 31
Position of the first harmonic number > 5: 83
Position of the first harmonic number > 6: 227
Position of the first harmonic number > 7: 616
Position of the first harmonic number > 8: 1674
Position of the first harmonic number > 9: 4550
Position of the first harmonic number > 10: 12367
</pre>
 
=={{header|C++}}==
3,028

edits