Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,15 @@
PROGRAM CATALAN
PROCEDURE CATALAN(N->RES)
RES=1
FOR I=1 TO N DO
RES=RES*2*(2*I-1)/(I+1)
END FOR
END PROCEDURE
BEGIN
FOR N=0 TO 15 DO
CATALAN(N->RES)
PRINT(N;"=";RES)
END FOR
END PROGRAM

View file

@ -0,0 +1,37 @@
(lib 'sequences)
(lib 'bigint)
(lib 'math)
;; function definition
(define (C1 n) (/ (factorial (* n 2)) (factorial (1+ n)) (factorial n)))
(for ((i [1 .. 16])) (write (C1 i)))
→ 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
;; using a recursive procedure with memoization
(define (C2 n) ;; ( Σ ...)is the same as (sigma ..)
(Σ (lambda(i) (* (C2 i) (C2 (- n i 1)))) 0 (1- n)))
(remember 'C2 #(1)) ;; first term defined here
(for ((i [1 .. 16])) (write (C2 i)))
→ 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
;; using procrastinators = infinite sequence
(define (catalan n acc) (/ (* acc 2 (1- (* 2 n))) (1+ n)))
(define C3 (scanl catalan 1 [1 ..]))
(take C3 15)
→ (1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845)
;; the same, using infix notation
(lib 'match)
(load 'infix.glisp)
(define (catalan n acc) ((2 * acc * ( 2 * n - 1)) / (n + 1)))
(define C3 (scanl catalan 1 [1 ..]))
(take C3 15)
→ (1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845)
;; or
(for ((c C3) (i 15)) (write c))
→ 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845

View file

@ -0,0 +1,38 @@
' FB 1.05.0 Win64
Function factorial(n As UInteger) As UInteger
If n = 0 Then Return 1
Return n * factorial(n - 1)
End Function
Function catalan1(n As UInteger) As UInteger
Dim prod As UInteger = 1
For i As UInteger = n + 2 To 2 * n
prod *= i
Next
Return prod / factorial(n)
End Function
Function catalan2(n As UInteger) As UInteger
If n = 0 Then Return 1
Dim sum As UInteger = 0
For i As UInteger = 0 To n - 1
sum += catalan2(i) * catalan2(n - 1 - i)
Next
Return sum
End Function
Function catalan3(n As UInteger) As UInteger
If n = 0 Then Return 1
Return catalan3(n - 1) * 2 * (2 * n - 1) \ (n + 1)
End Function
Print "n", "First", "Second", "Third"
Print "-", "-----", "------", "-----"
Print
For i As UInteger = 0 To 15
Print i, catalan1(i), catalan2(i), catalan3(i)
Next
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,22 @@
import integers.choose
import util.TextTable
def
catalan( n ) = choose( 2n, n )/(n + 1)
catalan2( n ) = product( (n + k)/k | k <- 2..n )
catalan3( 0 ) = 1
catalan3( n ) = 2*(2n - 1)/(n + 1)*catalan3( n - 1 )
t = TextTable()
t.header( 'n', 'definition', 'product', 'recursive' )
t.line()
for i <- 1..4
t.rightAlignment( i )
for i <- 0..15
t.row( i, catalan(i), catalan2(i), catalan3(i) )
println( t )

View file

@ -0,0 +1,17 @@
PROCEDURE Main()
LOCAL i
FOR i := 0 to 15
? PadL( i, 2 ) + ": " + hb_StrFormat("%d", Catalan( i ))
NEXT
RETURN
STATIC FUNCTION Catalan( n )
LOCAL i, nCatalan := 1
FOR i := 1 TO n
nCatalan := nCatalan * 2 * (2 * i - 1) / (i + 1)
NEXT
RETURN nCatalan

View file

@ -0,0 +1,33 @@
import strutils
proc binomial(m, n): auto =
result = 1
var
d = m - n
n = n
m = m
if d > n:
n = d
while m > n:
result *= m
dec m
while d > 1 and (result mod d) == 0:
result = result div d
dec d
proc catalan1(n): auto =
binomial(2 * n, n) div (n + 1)
proc catalan2(n): auto =
if n == 0:
result = 1
for i in 0 .. <n:
result += catalan2(i) * catalan2(n - 1 - i)
proc catalan3(n): int =
if n > 0: 2 * (2 * n - 1) * catalan3(n - 1) div (1 + n)
else: 1
for i in 0..15:
echo align($i, 7), " ", align($catalan1(i), 7), " ", align($catalan2(i), 7), " ", align($catalan3(i), 7)

View file

@ -0,0 +1 @@
: catalan(n) n ifZero: [ 1 ] else: [ catalan(n 1-) 2 n * 1- * 2 * n 1+ / ] ;

View file

@ -0,0 +1,53 @@
-- returns inf/-nan for n>85, and needs the rounding for n>=14, accurate to n=29
function catalan1(integer n)
return floor(factorial(2*n)/(factorial(n+1)*factorial(n))+0.5)
end function
-- returns inf for n>519, accurate to n=30:
function catalan2(integer n) -- NB: very slow!
atom res = not n
n -= 1
for i=0 to n do
res += catalan2(i)*catalan2(n-i)
end for
return res
end function
-- returns inf for n>514, accurate to n=30:
function catalan3(integer n)
if n=0 then return 1 end if
return 2*(2*n-1)/(1+n)*catalan3(n-1)
end function
for i=0 to 15 do
printf(1,"%2d: %10d %10d %10d\n",{i,catalan1(i),catalan2(i),catalan3(i)})
end for
-- An explicitly memoized version of what seems to be the best, and the one that really needed it:
-- (and in fact it turned out to be faster than similarly memoized versions of 1 and 3, when atom)
-- I also converted this to use bigatoms.
include builtins\bigatom.e
sequence c2cache = {}
function catalan2bc(integer n) -- very fast!
object r -- result (a bigatom)
if n<=0 then return BA_ONE end if
if n<=length(c2cache) then
r = c2cache[n]
if r!=0 then return r end if
else
c2cache &= repeat(0,n-length(c2cache))
end if
r = BA_ZERO
for i=0 to n-1 do
r = ba_add(r,ba_multiply(catalan2bc(i),catalan2bc(n-1-i)))
end for
c2cache[n] = r
return r
end function
atom t0 = time() -- (this last call only)
string sc100 = ba_sprint(catalan2bc(100))
printf(1,"100: %s (%3.2fs)\n",{sc100,time()-t0})

View file

@ -0,0 +1,8 @@
for n = 1 to 15
see catalan(n) + nl
next
func catalan n
if n = 0 return 1 ok
cat = 2 * (2 * n - 1) * catalan(n - 1) / (n + 1)
return cat

View file

@ -0,0 +1,2 @@
func f(i) { i==0 ? 1 : (i * f(i-1)) }
func c(n) { f(2*n) / f(n) / f(n+1) }

View file

@ -0,0 +1,3 @@
func c(n) is cached {
n == 0 ? 1 : (c(n-1) * (4 * n - 2) / (n + 1));
}

View file

@ -0,0 +1,3 @@
15.times { |i|
say "#{i-1}\t#{c(i-1)}";
}

View file

@ -0,0 +1,6 @@
; the following number expression calculcates the nth Catalan number
#~ddiFSFmSoFSn
; which stands for: dup dup inc fac swap fac mult swap double fac swap divide
; to get the first 15 Catalan numbers we map this function over a list from 0 to 15
!*#~ddiFSFmSoFSn @til 15
; returns [1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674439.9999999995]

View file

@ -0,0 +1,5 @@
def catalan:
if . == 0 then 1
elif . < 0 then error("catalan is not defined on \(.)")
else (2 * (2*. - 1) * ((. - 1) | catalan)) / (. + 1)
end;

View file

@ -0,0 +1 @@
(range(0; 16), 100) as $i | $i | catalan | [$i, .]

View file

@ -0,0 +1,18 @@
$ jq -M -n -c -f Catalan_numbers.jq
[0,1]
[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]
[100,8.96519947090131e+56]

View file

@ -0,0 +1,8 @@
def catalan_series(max):
def _catalan: # state: [n, catalan(n)]
if .[0] > max then empty
else .,
((.[0] + 1) as $n | .[1] as $cp
| [$n, (2 * (2*$n - 1) * $cp) / ($n + 1) ] | _catalan)
end;
[0,1] | _catalan;

View file

@ -0,0 +1 @@
catalan_series(15)

View file

@ -0,0 +1,4 @@
[0,1]
| recurse( if .[0] == 15 then empty
else .[1] as $c | (.[0] + 1) | [ ., (2 * (2*. - 1) * $c) / (. + 1) ]
end )