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
53
Task/Catalan-numbers/Phix/catalan-numbers.phix
Normal file
53
Task/Catalan-numbers/Phix/catalan-numbers.phix
Normal 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})
|
||||
Loading…
Add table
Add a link
Reference in a new issue