Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,38 @@
|
|||
PROGRAM CATALAN
|
||||
|
||||
!$DOUBLE
|
||||
|
||||
DIM CATALAN[50]
|
||||
|
||||
FUNCTION ODD(X)
|
||||
ODD=FRC(X/2)<>0
|
||||
END FUNCTION
|
||||
|
||||
PROCEDURE GETCATALAN(L)
|
||||
LOCAL J,K,W
|
||||
LOCAL DIM PASTRI[100]
|
||||
|
||||
L=L*2
|
||||
PASTRI[0]=1
|
||||
J=0
|
||||
WHILE J<L DO
|
||||
J+=1
|
||||
K=INT((J+1)/2)
|
||||
PASTRI[K]=PASTRI[K-1]
|
||||
FOR W=K TO 1 STEP -1 DO
|
||||
PASTRI[W]+=PASTRI[W-1]
|
||||
END FOR
|
||||
IF NOT(ODD(J)) THEN
|
||||
K=INT(J/2)
|
||||
CATALAN[K]=PASTRI[K]-PASTRI[K-1]
|
||||
END IF
|
||||
END WHILE
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
LL=15
|
||||
GETCATALAN(LL)
|
||||
FOR I=1 TO LL DO
|
||||
WRITE("### ####################";I;CATALAN[I])
|
||||
END FOR
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
(define dim 100)
|
||||
(define-syntax-rule (Tidx i j) (+ i (* dim j)))
|
||||
|
||||
;; generates Catalan's triangle
|
||||
;; T (i , j) = T(i-1,j) + T (i, j-1)
|
||||
|
||||
(define (T n)
|
||||
(define i (modulo n dim))
|
||||
(define j (quotient n dim))
|
||||
(cond
|
||||
((zero? i) 1) ;; left column = 1
|
||||
((= i j) (T (Tidx (1- i) j))) ;; diagonal value = left value
|
||||
(else (+ (T (Tidx (1- i) j)) (T (Tidx i (1- j)))))))
|
||||
|
||||
(remember 'T #(1))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
;; take elements on diagonal = Catalan numbers
|
||||
(for ((i (in-range 0 16))) (write (T (Tidx i i))))
|
||||
|
||||
→ 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
' version 15-09-2015
|
||||
' 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 <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
const n = 15
|
||||
var t = newSeq[int](n + 2)
|
||||
|
||||
t[1] = 1
|
||||
for i in 1..n:
|
||||
for j in countdown(i, 1): t[j] += t[j-1]
|
||||
t[i+1] = t[i]
|
||||
for j in countdown(i+1, 1): t[j] += t[j-1]
|
||||
stdout.write t[i+1] - t[i], " "
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
: pascal(n) [ 1 ] #[ dup 0 + 0 rot + zipWith(#+) ] times(n) ;
|
||||
: catalan(n) pascal(n 2 * ) at(n 1+) n 1+ / ;
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
constant N = 15 -- accurate to 30, nan/inf for anything over 514 (bigatom version is below).
|
||||
sequence catalan = {}, -- (>=1 only)
|
||||
p = repeat(1,N+1)
|
||||
atom p1
|
||||
for i=1 to N do
|
||||
p1 = p[1]*2
|
||||
catalan = append(catalan,p1-p[2])
|
||||
for j=1 to N-i+1 do
|
||||
p1 += p[j+1]
|
||||
p[j] = p1
|
||||
end for
|
||||
-- ?p[1..N-i+1]
|
||||
end for
|
||||
?catalan
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
-- FreeBASIC said:
|
||||
--' 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) ...
|
||||
--
|
||||
-- The first thing that struck me was it is twice as big as it needs to be,
|
||||
-- something like this would do...
|
||||
-- 1 1 1 1 1 1
|
||||
-- 2 3 4 5 6
|
||||
-- 6 10 15 21
|
||||
-- 20 35 56
|
||||
-- 70 126
|
||||
-- 252
|
||||
-- It is more obvious from the upper square that the diagonal on that, which is
|
||||
-- that same as column 1 on this, is twice the previous, which on the second
|
||||
-- diagram is in column 2. Further, once we have calculated the value for column
|
||||
-- one above, we can use it immediately to calculate the next catalan number and
|
||||
-- do not need to store it. Lastly we can overwrite row 1 with row 2 etc in situ,
|
||||
-- and the following shows what we need for subsequent rounds:
|
||||
-- 1 1 1 1 1
|
||||
-- 3 4 5 6
|
||||
-- 10 15 21
|
||||
-- 35 56
|
||||
-- 126 (unused)
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
include builtins\bigatom.e
|
||||
|
||||
function catalanB(integer n) -- very very fast!
|
||||
sequence catalan = {},
|
||||
p = repeat(1,n+1)
|
||||
bigatom p1
|
||||
if n=0 then return 1 end if
|
||||
for i=1 to n do
|
||||
p1 = ba_multiply(p[1],2)
|
||||
catalan = append(catalan,ba_sub(p1,p[2]))
|
||||
for j=1 to n-i+1 do
|
||||
p1 = ba_add(p1,p[j+1])
|
||||
p[j] = p1
|
||||
end for
|
||||
end for
|
||||
return catalan[n]
|
||||
end function
|
||||
|
||||
atom t0 = time()
|
||||
string sc100 = ba_sprint(catalanB(100))
|
||||
printf(1,"%d: %s (%3.2fs)\n",{100,sc100,time()-t0})
|
||||
atom t0 = time()
|
||||
string sc250 = ba_sprint(catalanB(250))
|
||||
printf(1,"%d: %s (%3.2fs)\n",{250,sc250,time()-t0})
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
n=15
|
||||
cat = list(n+2)
|
||||
cat[1]=1
|
||||
for i=1 to n
|
||||
for j=i+1 to 2 step -1
|
||||
cat[j]=cat[j]+cat[j-1]
|
||||
next
|
||||
cat[i+1]=cat[i]
|
||||
for j=i+2 to 2 step -1
|
||||
cat[j]=cat[j]+cat[j-1]
|
||||
next
|
||||
see "" + (cat[i+1]-cat[i]) + " "
|
||||
next
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
func catalan(num) {
|
||||
var t = [0, 1];
|
||||
range(1, num).map { |i|
|
||||
range(i, 1, -1).each {|j| t[j] += t[j-1]};
|
||||
t[i+1] = t[i];
|
||||
range(i+1, 1, -1).each {|j| t[j] += t[j-1]};
|
||||
t[i+1] - t[i];
|
||||
}
|
||||
}
|
||||
|
||||
say catalan(15).join(' ');
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def binomial(n; k):
|
||||
if k > n / 2 then binomial(n; n-k)
|
||||
else reduce range(1; k+1) as $i (1; . * (n - $i + 1) / $i)
|
||||
end;
|
||||
|
||||
# Direct (naive) computation using two numbers in Pascal's triangle:
|
||||
def catalan_by_pascal: . as $n | binomial(2*$n; $n) - binomial(2*$n; $n-1);
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
$ jq -n -c -f Catalan_numbers_Pascal.jq
|
||||
[0,0]
|
||||
[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]
|
||||
[30,3814986502092304]
|
||||
[31,14544636039226880]
|
||||
[510,5.491717746183512e+302]
|
||||
[511,null]
|
||||
Loading…
Add table
Add a link
Reference in a new issue