48 lines
1.0 KiB
Plaintext
48 lines
1.0 KiB
Plaintext
include "NSLog.incl"
|
|
|
|
local fn Factorial( n as NSInteger ) as UInt64
|
|
UInt64 sum = 0
|
|
|
|
if n = 0 then sum = 1 : exit fn
|
|
sum = n * fn Factorial( n - 1 )
|
|
end fn = sum
|
|
|
|
local fn Catalan1( n as NSInteger ) as UInt64
|
|
UInt64 product = 1, result
|
|
NSUInteger i
|
|
|
|
for i = n + 2 to 2 * n
|
|
product = product * i
|
|
next
|
|
result = product / fn Factorial( n )
|
|
end fn = result
|
|
|
|
local fn Catalan2( n as NSInteger ) as UInt64
|
|
UInt64 sum = 0
|
|
NSUInteger i
|
|
|
|
if n = 0 then sum = 1 : exit fn
|
|
for i = 0 to n - 1
|
|
sum += fn Catalan2(i) * fn Catalan2( n - 1 - i )
|
|
next
|
|
end fn = sum
|
|
|
|
local fn Catalan3( n as NSInteger ) as UInt64
|
|
UInt64 result
|
|
|
|
if n = 0 then result = 1 : exit fn
|
|
result = fn Catalan3( n - 1 ) * 2 * ( 2 * n - 1 ) / ( n + 1 )
|
|
end fn = result
|
|
|
|
NSUInteger i
|
|
|
|
for i = 0 to 19
|
|
if( i < 16 )
|
|
NSLog( @"%3d.\t\t%7llu\t\t%12llu\t\t%12llu", i, fn Catalan1( i ), fn Catalan2( i ), fn Catalan3( i ) )
|
|
else
|
|
NSLog( @"%3d.\t\t%@\t\t%12llu\t\t%12llu", i, @"[-err-]", fn Catalan2( i ), fn Catalan3( i ) )
|
|
end if
|
|
next
|
|
|
|
HandleEvents
|