Find limit of recursion: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) (Added Arturo implementation) |
m (→{{header|Phix}}: added syntax colouring the hard way, phix/basics) |
||
Line 2,212: | Line 2,212: | ||
On 32-bit the limit is an impressive 31 million. I have seen this hit 43 million on 64-bit, but it then forced a hard reboot.<br> |
On 32-bit the limit is an impressive 31 million. I have seen this hit 43 million on 64-bit, but it then forced a hard reboot.<br> |
||
Those limits will obviously be significantly smaller for routines with more parameters, local variables, and temps. |
Those limits will obviously be significantly smaller for routines with more parameters, local variables, and temps. |
||
<lang Phix> |
<!--<lang Phix>--> |
||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">+<span style="color: #000000;">1</span> |
|||
integer depth = 0 |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">depth</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> |
|||
procedure recurse() |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">recurse<span style="color: #0000FF;">(<span style="color: #0000FF;">)</span> |
|||
if time()>t1 then |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span> |
|||
?depth |
|||
<span style="color: #0000FF;">?<span style="color: #000000;">depth</span> |
|||
t1 = time()+1 |
|||
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">+<span style="color: #000000;">1</span> |
|||
end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
depth += 1 |
|||
<span style="color: #000000;">depth</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
|||
-- only 1 of these will ever get called, of course... |
<span style="color: #000080;font-style:italic;">-- only 1 of these will ever get called, of course...</span> |
||
recurse() |
|||
<span style="color: #000000;">recurse<span style="color: #0000FF;">(<span style="color: #0000FF;">)</span> |
|||
recurse() |
|||
<span style="color: #000000;">recurse<span style="color: #0000FF;">(<span style="color: #0000FF;">)</span> |
|||
recurse() |
|||
<span style="color: #000000;">recurse<span style="color: #0000FF;">(<span style="color: #0000FF;">)</span> |
|||
end procedure |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
recurse()</lang> |
|||
<span style="color: #000000;">recurse<span style="color: #0000FF;">(<span style="color: #0000FF;">) |
|||
<!--</lang>--> |
|||
{{out|output|text= 32 bit}} |
{{out|output|text= 32 bit}} |
||
<pre> |
<pre> |
||
Line 2,272: | Line 2,274: | ||
=== saner === |
=== saner === |
||
The following much more safely merely tests it can reach 20,000,000, plus however far it gets in the last whole-second |
The following much more safely merely tests it can reach 20,000,000, plus however far it gets in the last whole-second |
||
<lang Phix> |
<!--<lang Phix>--> |
||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">+<span style="color: #000000;">1</span> |
|||
integer depth = 0, depth_blown = false |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">depth</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0<span style="color: #0000FF;">,</span> <span style="color: #000000;">depth_blown</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span> |
|||
string btd = "building" |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">btd</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"building"</span> |
|||
procedure recurse() |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">recurse<span style="color: #0000FF;">(<span style="color: #0000FF;">)</span> |
|||
if time()>t1 then |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span> |
|||
printf(1,"depth: %d (%s)\n",{depth,btd}) |
|||
<span style="color: #7060A8;">printf<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"depth: %d (%s)\n"<span style="color: #0000FF;">,<span style="color: #0000FF;">{<span style="color: #000000;">depth<span style="color: #0000FF;">,<span style="color: #000000;">btd<span style="color: #0000FF;">}<span style="color: #0000FF;">)</span> |
|||
if depth>20_000_000 then |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">depth<span style="color: #0000FF;">><span style="color: #000000;">20<span style="color: #000000;">_000_000</span> <span style="color: #008080;">then</span> |
|||
depth_blown = true |
|||
<span style="color: #000000;">depth_blown</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span> |
|||
btd = "tearing down" |
|||
<span style="color: #000000;">btd</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"tearing down"</span> |
|||
end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
t1 = time()+1 |
|||
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">+<span style="color: #000000;">1</span> |
|||
end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
if depth_blown then |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">depth_blown</span> <span style="color: #008080;">then</span> |
|||
depth -= 1 |
|||
<span style="color: #000000;">depth</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span> |
|||
else |
|||
<span style="color: #008080;">else</span> |
|||
depth += 1 |
|||
<span style="color: #000000;">depth</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
|||
recurse() -- (build, aka +1 with progress) |
|||
<span style="color: #000000;">recurse<span style="color: #0000FF;">(<span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (build, aka +1 with progress)</span> |
|||
recurse() -- (tear down, -1 with progress) |
|||
<span style="color: #000000;">recurse<span style="color: #0000FF;">(<span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (tear down, -1 with progress)</span> |
|||
end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end procedure |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
recurse()</lang> |
|||
<span style="color: #000000;">recurse<span style="color: #0000FF;">(<span style="color: #0000FF;">) |
|||
<!--</lang>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |