Catalan numbers/Pascal's triangle: Difference between revisions
Content added Content deleted
(Added XPL0 example.) |
(RPL: add section) |
||
Line 1,981: | Line 1,981: | ||
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 |
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 |
||
</pre> |
</pre> |
||
=={{header|RPL}}== |
|||
We have taken the opportunity provided by the task to develop a routine that recursively computes Pascal's triangle. |
|||
If the challenge was to look for high Catalan numbers, memoizing the triangle would make sense. |
|||
{{works with|Halcyon Calc|4.2.8}} |
|||
{| class="wikitable" |
|||
! RPL code |
|||
! Comment |
|||
|- |
|||
| |
|||
≪ '''IF THEN''' LAST 1 + → n1 |
|||
≪ n1 2 - '''PASLIN''' |
|||
{ } n1 + RDM |
|||
DUP ARRY→ DROP n1 ROLLD n1 →ARRY + |
|||
≫ '''ELSE''' [ 1 ] '''END''' |
|||
≫ ‘'''PASLIN'''’ STO |
|||
≪ |
|||
DUP DUP + '''PASLIN''' |
|||
SWAP GETI ROT ROT GET SWAP - |
|||
≫ ‘'''CATLN'''’ STO |
|||
| |
|||
'''PASLIN''' ''( n -- [ 1..(n,p)..1 ] ) '' |
|||
get previous line |
|||
add a zero at tail |
|||
duplicate it, rotate right coeffs and sum |
|||
'''CATLN''' ''( n -- C(n) )'' |
|||
get Pascal_line(2n) |
|||
return line[n+1]-line[n] |
|||
|} |
|||
{{in}} |
|||
<pre> |
|||
≪ { } 1 15 FOR j CATLN j + NEXT ≫ EVAL |
|||
</pre> |
|||
{{out}} |
|||
<pre> |
|||
1: { 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 } |
|||
</pre> |
|||
Runs in 119 seconds on a HP-28S. |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang="tcl">def catalan(num) |
<syntaxhighlight lang="tcl">def catalan(num) |
||
Line 1,997: | Line 2,040: | ||
[1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845] |
[1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845] |
||
</pre> |
</pre> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
<syntaxhighlight lang="runbasic">n = 15 |
<syntaxhighlight lang="runbasic">n = 15 |