September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,10 @@
begin
% print the catalan numbers up to C15 %
integer Cprev;
Cprev := 1; % C0 %
write( s_w := 0, i_w := 3, 0, ": ", i_w := 9, Cprev );
for n := 1 until 15 do begin
Cprev := round( ( ( ( 4 * n ) - 2 ) / ( n + 1 ) ) * Cprev );
write( s_w := 0, i_w := 3, n, ": ", i_w := 9, Cprev );
end for_n
end.

View file

@ -0,0 +1,17 @@
10 FOR N=0 TO 14
20 LET X=N
30 GOSUB 130
40 LET A=FX
50 LET X=N+1
60 GOSUB 130
70 LET B=FX
80 LET X=2*N
90 GOSUB 130
100 PRINT N,FX/(B*A)
110 NEXT N
120 STOP
130 LET FX=1
140 FOR I=1 TO X
150 LET FX=FX*I
160 NEXT I
170 RETURN

View file

@ -1,16 +0,0 @@
// Generate Catalan Numbers
//
// Nigel Galloway: June 9th., 2012
//
#include <iostream>
int main() {
const int N = 15;
int t[N+2] = {0,1};
for(int i = 1; i<=N; i++){
for(int j = i; j>1; j--) t[j] = t[j] + t[j-1];
t[i+1] = t[i];
for(int j = i+1; j>1; j--) t[j] = t[j] + t[j-1];
std::cout << t[i+1] - t[i] << " ";
}
return 0;
}

View file

@ -1,19 +0,0 @@
class
CATALAN_NUMBERS
feature
cat_num(n:INTEGER): DOUBLE
require
n_not_negative:n>= 0
local
s,t: DOUBLE
do
if n=0 then
Result:= 1.0
else
t:= 4*n.to_double-2
s:= n.to_double+1
Result:= t/s*cat_num(n-1)
end
end
end

View file

@ -1,24 +0,0 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
local
s: STRING
do
create cat
from
i:=0
until
i>14
loop
io.put_double (cat.cat_num (i))
io.put_string ("%N")
i:= i+1
end
end
cat: CATALAN_NUMBERS
end

View file

@ -0,0 +1,23 @@
class Catalan
{
public static void main(String[] args)
{
BigInteger N = 15;
BigInteger k,n,num,den;
BigInteger catalan;
print(1);
for(n=2;n<=N;n++)
{
num = 1;
den = 1;
for(k=2;k<=n;k++)
{
num = num*(n+k);
den = den*k;
catalan = num/den;
}
println(catalan);
}
}
}

View file

@ -1,29 +0,0 @@
<html><head><title>Catalan</title></head>
<body><pre id='x'></pre><script type="application/javascript">
function disp(x) {
var e = document.createTextNode(x + '\n');
document.getElementById('x').appendChild(e);
}
var fc = [], c2 = [], c3 = [];
function fact(n) { return fc[n] ? fc[n] : fc[n] = (n ? n * fact(n - 1) : 1); }
function cata1(n) { return Math.floor(fact(2 * n) / fact(n + 1) / fact(n) + .5); }
function cata2(n) {
if (n == 0) return 1;
if (!c2[n]) {
var s = 0;
for (var i = 0; i < n; i++) s += cata2(i) * cata2(n - i - 1);
c2[n] = s;
}
return c2[n];
}
function cata3(n) {
if (n == 0) return 1;
return c3[n] ? c3[n] : c3[n] = (4 * n - 2) * cata3(n - 1) / (n + 1);
}
disp(" meth1 meth2 meth3");
for (var i = 0; i <= 15; i++)
disp(i + '\t' + cata1(i) + '\t' + cata2(i) + '\t' + cata3(i));
</script></body></html>

View file

@ -1,17 +0,0 @@
meth1 meth2 meth3
0 1 1 1
1 1 1 1
2 2 2 2
3 5 5 5
4 14 14 14
5 42 42 42
6 132 132 132
7 429 429 429
8 1430 1430 1430
9 4862 4862 4862
10 16796 16796 16796
11 58786 58786 58786
12 208012 208012 208012
13 742900 742900 742900
14 2674440 2674440 2674440
15 9694845 9694845 9694845

View file

@ -1,33 +1,31 @@
import net.openhft.koloboke.collect.map.hash.HashIntDoubleMaps.*
abstract class Catalan {
abstract operator fun invoke(n: Int) : Double
protected val m = newUpdatableMapOf(0 , 1.0)
protected val m = mutableMapOf(0 to 1.0)
}
object CatalanI : Catalan() {
override fun invoke(n: Int): Double {
if (n !in m)
m[n] = Math.round(fact(2 * n) / (fact(n + 1) * fact(n))).toDouble()
return m[n]
return m[n]!!
}
private fun fact(n: Int): Double {
if (n in facts)
return facts[n]
var f = n * fact(n -1)
return facts[n]!!
val f = n * fact(n -1)
facts[n] = f
return f
}
private val facts = newUpdatableMapOf(0 , 1.0, 1 , 1.0, 2 , 2.0)
private val facts = mutableMapOf(0 to 1.0, 1 to 1.0, 2 to 2.0)
}
object CatalanR1 : Catalan() {
override fun invoke(n: Int): Double {
if (n in m)
return m[n]
return m[n]!!
var sum = 0.0
for (i in 0..n - 1)
@ -42,7 +40,7 @@ object CatalanR2 : Catalan() {
override fun invoke(n: Int): Double {
if (n !in m)
m[n] = Math.round(2.0 * (2 * (n - 1) + 1) / (n + 1) * invoke(n - 1)).toDouble()
return m[n]
return m[n]!!
}
}

View file

@ -1,3 +1,3 @@
function n = catalanNumbers(n)
n = prod(n+1:2*n)/prod(1:n+1);
n = arrayfun(@(n)prod(n+1:2*n)/prod(1:n+1), n);
end

View file

@ -0,0 +1,12 @@
let catalan : int ref = ref 0 in
Printf.printf "%d ," 1 ;
for i = 2 to 9 do
let nm : int ref = ref 1 in
let den : int ref = ref 1 in
for k = 2 to i do
nm := (!nm)*(i+k);
den := (!den)*k;
catalan := (!nm)/(!den) ;
done;
print_int (!catalan); print_string "," ;
done;;

View file

@ -0,0 +1,81 @@
loop i = 0 to 15
say "catI("i") =" .catalan~catI(i)
say "catR1("i") =" .catalan~catR1(i)
say "catR2("i") =" .catalan~catR2(i)
end
-- This is implemented as static members on a class object
-- so that the code is able to keep state information between calls. This
-- memoization will speed up things like factorial calls by remembering previous
-- results.
::class catalan
-- initialize the class object
::method init class
expose facts catI catR1 catR2
facts = .table~new
catI = .table~new
catR1 = .table~new
catR2 = .table~new
-- seed a few items
facts[0] = 1
facts[1] = 1
facts[2] = 2
catI[0] = 1
catR1[0] = 1
catR2[0] = 1
-- private factorial method
::method fact private class
expose facts
use arg n
-- see if we've calculated this before
if facts~hasIndex(n) then return facts[n]
numeric digits 120
fact = 1
loop i = 2 to n
fact *= i
end
-- save this result
facts[n] = fact
return fact
::method catI class
expose catI
use arg n
numeric digits 20
res = catI[n]
if res == .nil then do
-- dividing by 1 removes insignificant trailing 0s
res = (self~fact(2 * n)/(self~fact(n + 1) * self~fact(n))) / 1
catI[n] = res
end
return res
::method catR1 class
expose catR1
use arg n
numeric digits 20
if catR1~hasIndex(n) then return catR1[n]
sum = 0
loop i = 0 to n - 1
sum += self~catR1(i) * self~catR1(n - 1 - i)
end
-- remove insignificant trailing 0s
sum = sum / 1
catR1[n] = sum
return sum
::method catR2 class
expose catR2
use arg n
numeric digits 20
res = catR2[n]
if res == .nil then do
res = ((2 * (2 * n - 1) * self~catR2(n - 1)) / (n + 1))
catR2[n] = res
end
return res

View file

@ -1 +1 @@
.say for Catalan[^15];
.say for Catalan[^16];

View file

@ -10,7 +10,7 @@ call hdr 2 ; do j=LO to HI; say ' Catalan' right(j, w)": " Cat2(
call hdr 3 ; do j=LO to HI; say ' Catalan' right(j, w)": " Cat3(j) ; end
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: arg z; if !.z\==. then return !.z; !=1; do k=2 for z; !=!*k; end; !.z=!; return !
!: arg z; if !.z\==. then return !.z; !=1; do k=2 to z; !=!*k; end; !.z=!; return !
Cat1A: procedure expose !.; parse arg n; return comb(n+n, n) % (n+1)
Cat1B: procedure expose !.; parse arg n; return !(n+n) % ((n+1) * !(n)**2)
Cat3: procedure expose c.; arg n; if c.n==. then c.n=(4*n-2)*cat3(n-1)%(n+1); return c.n
@ -18,6 +18,6 @@ comb: procedure; parse arg x,y; return pFact(x-y+1, x) % pFact(2,
hdr: !.=.; c.=.; c.0=1; say; say center('Catalan numbers, method' arg(1),79,''); return
pFact: procedure; !=1; do k=arg(1) to arg(2); !=!*k; end; return !
/*──────────────────────────────────────────────────────────────────────────────────────*/
Cat2: procedure expose c.; parse arg n; $=0; if c.n\==. then return c.n
do k=0 to n-1; $=$ + cat2(k) * cat2(n-k-1); end
Cat2: procedure expose c.; parse arg n; $=0; if c.n\==. then return c.n
do k=0 for n; $=$ + Cat2(k) * Cat2(n-k-1); end
c.n=$; return $ /*use a memoization technique.*/

View file

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

View file

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

View file

@ -0,0 +1,30 @@
PRINT "Recursive:"!PRINT
FOR n = 0 TO 15
PRINT n,"#######":catrec(n)
NEXT n
PRINT!PRINT
PRINT "Non-recursive:"!PRINT
FOR n = 0 TO 15
PRINT n,"#######":catnonrec(n)
NEXT n
END
DEF catrec(x)
IF x = 0 THEN
temp = 1
ELSE
n = x
temp = ((2*((2*n)-1))/(n+1))*catrec(n-1)
END IF
catrec = temp
END DEF
DEF catnonrec(x)
temp = 1
FOR n = 1 TO x
temp = (2*((2*n)-1))/(n+1)*temp
NEXT n
catnonrec = temp
END DEF

View file

@ -0,0 +1,11 @@
(defun catalan (n)
(if (= n 0)
1
(* (/ (* 2 (- (* 2 n) 1)) (+ n 1)) (catalan (- n 1))) ) )
(defun range (x y)
(cons x
(if (< x y)
(range (+ x 1) y) ) ) )
(print (mapcar catalan (range 0 14)))

View file

@ -0,0 +1,9 @@
var BN=Import("zklBigNum");
fcn catalan(n){
BN(2*n).factorial() / BN(n+1).factorial() / BN(n).factorial();
}
foreach n in (16){
println("%2d --> %,d".fmt(n, catalan(n)));
}
println("%2d --> %,d".fmt(100, catalan(100)));

View file

@ -0,0 +1 @@
fcn catalan(n){ (1).reduce(n,fcn(p,n){ 2*(2*n-1)*p/(n+1) },1) }