Harmonic series: Difference between revisions

FORTH version
(Added Algol 68)
(FORTH version)
Line 236:
</pre>
 
=={{header|Forth}}==
Uses fixed point computation which is more traditional in FORTH.
<lang Forth>
warnings off
 
1.000.000.000.000.000 drop constant 1.0fx \ fractional part is 15 decimal digits.
 
: .h ( n -- )
s>d <# 14 for # next [char] . hold #s #> type space ;
 
1.0fx 1 2constant first-harmonic
: next-harmonic ( h n -- h' n' )
1+ tuck 1.0fx swap / + swap ;
 
: task1
first-harmonic 19 for over cr .h next-harmonic next 2drop ;
 
: task2
first-harmonic
11 1 do
begin over i 1.0fx * <= while
next-harmonic
repeat
dup .
loop 2drop ;
 
." The first 10 harmonic numbers: " task1 cr cr
." The indices of the first harmonic number that exceeds the nth integer: " cr task2 cr
bye
</lang>
{{Out}}
<pre>
The first 10 harmonic numbers:
1.000000000000000
1.500000000000000
1.833333333333333
2.083333333333333
2.283333333333333
2.449999999999999
2.592857142857141
2.717857142857141
2.828968253968252
2.928968253968252
3.019877344877342
3.103210678210675
3.180133755133751
3.251562326562322
3.318228993228988
3.380728993228988
3.439552522640752
3.495108078196307
3.547739657143675
3.597739657143675
 
The indices of the first harmonic number that exceeds the nth integer:
2 4 11 31 83 227 616 1674 4550 12367
</pre>
=={{header|Go}}==
{{trans|Wren}}
357

edits