Find limit of recursion: Difference between revisions

Content added Content deleted
m (→‎recursive procedure: added some wording to the REXX section header.)
(++ Dc)
Line 701: Line 701:


Using -O compilation argument DMD performs tail call optimization, and the stack doesn't overflow.
Using -O compilation argument DMD performs tail call optimization, and the stack doesn't overflow.

=={{header|Dc}}==
Tail recursion is optimized into iteration by GNU dc, so I designed a not tail recursive function, summing all numbers up to n:
<lang dc>## f(n) = (n < 1) ? n : f(n-1) + n;
[q]sg
[dSn d1[>g 1- lfx]x Ln+]sf
[ [n=]Pdn []pP lfx [--> ]P p ]sh

65400 lhx
65600 lhx</lang>
With the standard Ubuntu stack size limit 8MB I get
{{out}}
<pre>$ time dc t1.dc
n=65400
--> 2138612700
n=65600
Segmentation fault (core dumped)

real 0m0.337s
user 0m0.112s
sys 0m0.008s
</pre>
With larger stack size limit I get linearly greater numbers: stack size / 128.


=={{header|Déjà Vu}}==
=={{header|Déjà Vu}}==