Catalan numbers/Pascal's triangle: Difference between revisions

added FreeBasic example
m (Added the Sidef language)
(added FreeBasic example)
Line 316:
15 9694845
</pre>
=={{header|FreeBASIC}}==
<lang freebasic>' version 15-092015
' compile with: fbc -s console
 
#Define size 31 ' (N * 2 + 1)
 
Sub pascal_triangle(rows As Integer, Pas_tri() As ULongInt)
 
Dim As Integer x, y
 
For x = 1 To rows
Pas_tri(1,x) = 1
Pas_tri(x,1) = 1
Next
 
For x = 2 To rows
For y = 2 To rows + 1 - x
Pas_tri(x, y) = pas_tri(x - 1 , y) + pas_tri(x, y - 1)
Next
Next
 
End Sub
 
' ------=< MAIN >=------
 
Dim As Integer count, row
Dim As ULongInt triangle(1 To size, 1 To size)
 
pascal_triangle(size, triangle())
 
' 1 1 1 1 1 1
' 1 2 3 4 5 6
' 1 3 6 10 15 21
' 1 4 10 20 35 56
' 1 5 15 35 70 126
' 1 6 21 56 126 252
' The Pascal triangle is rotated 45 deg.
' to find the Catalan number we need to follow the diagonal
' for top left to bottom right
' take the number on diagonal and subtract the number in de cell
' one up and one to right
' 1 (2 - 1), 2 (6 - 4), 5 (20 - 15) ...
 
 
Print "The first 15 Catalan numbers are" : print
count = 1 : row = 2
Do
Print Using "###: #########"; count; triangle(row, row) - triangle(row +1, row -1)
row = row + 1
count = count + 1
Loop Until count > 15
 
' empty keyboard buffer
While InKey <> "" : Var _key_ = InKey : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>The first 15 Catalan numbers are
 
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|Haskell}}==
457

edits