Catalan numbers/Pascal's triangle: Difference between revisions
Content added Content deleted
(→Python :: Composition of pure functions: docstring edit) |
m (→{{header|Phix}}: bigatom -> mpfr) |
||
Line 1,438: | Line 1,438: | ||
-- 35 56 |
-- 35 56 |
||
-- 126 (unused)</lang> |
-- 126 (unused)</lang> |
||
⚫ | |||
=== gmp version === |
|||
{{libheader| |
{{libheader|mpfr}} |
||
<lang Phix>include builtins\ |
<lang Phix>include builtins\mpfr.e |
||
function catalanB(integer n) -- very very fast! |
function catalanB(integer n) -- very very fast! |
||
sequence catalan = |
sequence catalan = mpz_inits(n), |
||
p = |
p = mpz_inits(n+1,1) |
||
mpz p1 = mpz_init(1) |
|||
bigatom p1 |
|||
if n=0 then return |
if n=0 then return p1 end if |
||
for i=1 to n do |
for i=1 to n do |
||
mpz_mul_si(p1,p[1],2) |
|||
mpz_sub(catalan[i],p1,p[2]) |
|||
for j=1 to n-i+1 do |
for j=1 to n-i+1 do |
||
mpz_add(p1,p1,p[j+1]) |
|||
p[j] |
mpz_set(p[j],p1) |
||
end for |
end for |
||
end for |
end for |
||
Line 1,458: | Line 1,459: | ||
end function |
end function |
||
⚫ | |||
atom t0 = time() |
|||
printf(1,"%d: %s (%s)\n",{250,mpz_get_str(catalanB(250))})</lang> |
|||
⚫ | |||
atom t0 = time() |
|||
string sc250 = ba_sprint(catalanB(250)) |
|||
printf(1,"%d: %s (%3.2fs)\n",{250,sc250,time()-t0})</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
100: 896519947090131496687170070074100632420837521538745909320 |
100: 896519947090131496687170070074100632420837521538745909320 |
||
250: 465116795969233796497747947259667807407291160080922096111953326525143875193659257831340309862635877995262413955019878805418475969029457769094808256 |
250: 465116795969233796497747947259667807407291160080922096111953326525143875193659257831340309862635877995262413955019878805418475969029457769094808256 |
||
</pre> |
|||
⚫ | |||
a quick comparison showing the latter getting exponentially worse (then again I memoised the slowest recursive version): |
|||
<pre> |
|||
800 2000 4000 8000 |
|||
catalanB: 0.6s 3.5s 14.5s 64s |
|||
catalan2m: 0.7s 7.0s 64.9s 644s |
|||
</pre> |
</pre> |
||