Catalan numbers/Pascal's triangle: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,039: | Line 1,039: | ||
{{out}} |
{{out}} |
||
<pre>1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440</pre> |
<pre>1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440</pre> |
||
=={{header|M2000 Interpreter}}== |
|||
{{trans|FreeBasic}} |
|||
We have to add -1 in For x=2 to rows, because in FreeBasic when x=rows then inner loop never happen because end value for y is 1, so lower than start value 2. In M2000 this should run from 2 to 1, so we have to exclude this situation from outer loop, adding -1, and before loop we have to include en exit from sub if rows are less than 2. |
|||
We can define integer variables (16 bit), and we can use integer literals numbers using % as last char. |
|||
Inside triangle array we use decimal numbers, using @ for first literals, so all additions next produce decimals too. |
|||
We use & to pass by reference, here anarray, to sub, but because a sub can see anything in module we can change array name inside sub to same as triangle and we can remove arguments (including size). |
|||
<lang M2000 Interpreter> |
|||
Module CatalanNumbers { |
|||
Def Integer count, t_row, size=31 |
|||
Dim triangle(1 to size, 1 to size) |
|||
\\ call sub |
|||
pascal_triangle(size, &triangle()) |
|||
Print "The first 15 Catalan numbers are" |
|||
count = 1% : t_row = 2% |
|||
Do { |
|||
Print Format$("{0:0:-3}:{1:0:-15}", count, triangle(t_row, t_row) - triangle(t_row +1, t_row -1)) |
|||
t_row++ |
|||
count++ |
|||
} Until count > 15 |
|||
End |
|||
Sub pascal_triangle(rows As Integer, &Pas_tri()) |
|||
Local x=0%, y=0% |
|||
For x = 1 To rows |
|||
Pas_tri( 1%, x ) = 1@ |
|||
Pas_tri( x, 1% ) = 1@ |
|||
Next x |
|||
if rows<2 then exit sub |
|||
For x = 2 To rows-1 |
|||
For y = 2 To rows + 1 - x |
|||
Pas_tri(x, y) = pas_tri(x - 1 , y) + pas_tri(x, y - 1) |
|||
Next y |
|||
Next x |
|||
End Sub |
|||
} |
|||
CatalanNumbers |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
1: 1 |
|||
2: 2 |
|||
3: 5 |
|||
4: 14 |
|||
5: 42 |
|||
6: 132 |
|||
7: 429 |
|||
8: 1430 |
|||
9: 4862 |
|||
10: 16796 |
|||
11: 58786 |
|||
12: 208012 |
|||
13: 742900 |
|||
14: 2674440 |
|||
15: 9694845 |
|||
</pre> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |