Harmonic series: Difference between revisions

RPL: add
(Added Lua version)
(RPL: add)
Line 1,943:
The first harmonic number greater than 10 is 10.000043008276, at position 12367
</pre>
 
=={{header|RPL}}==
≪ 0 1 ROT '''FOR''' j INV + '''NEXT'''≫ ‘'''HARMO'''’ STO
≪ 5 FIX { } 1 20 '''FOR''' n n + '''HARMO NEXT''' ≫ EVAL
{{out}}
<pre>
1: { 1.00000 1.50000 1.83333 2.08333 2.28333 2.45000 2.59286 2.71786 2.82897 2.92897 3.01988 3.10321 3.18013 3.25156 3.31823 3.38073 3.43955 3.49511 3.54774 3.59774 }
</pre>
 
We haved fulfilled the stretched part of the task on a vintage HP-28S, with the objective to be as fast as possible. H(n+1) is calculated directly from H(n), and a <code>FOR..NEXT</code> loop allows to reduce stack depth and eliminate the counter incrementation by the user; 9999 is a magic number used to exit the loop.
≪ → max
≪ { } 0 1 9999 '''FOR''' j
j INV +
'''IF''' OVER SIZE 1 + OVER <
'''THEN''' SWAP j +
'''IF''' DUP SIZE max > '''THEN''' 9999 'j' STO '''END'''
SWAP '''END'''
'''NEXT''' DROP
≫ ≫ ‘'''TASK2'''’ STO
{{out}}
<pre>
1: { 2 4 11 31 83 227 616 1674 4550 12367 }
</pre>
was returned in less than 8 minutes.
 
=={{header|Rust}}==
1,151

edits