Permutations/Derangements: Difference between revisions

m
m (→‎{{header|Phix}}: bigatom -> mpfr)
Line 2,472:
!20=895014631192902121 (mpfr)
</pre>
 
{{trans|FreeBASIC}}
A more efficient method of calculating subfactorials (0 should be handled separately, or obviously prepend a 1 and extract with idx+1).<br>
Should you instead of string results want an array of mpz for further calculations, use the mpz_init_set() call as shown:
<lang Phix>include mpfr.e
function subfactorial(integer n)
sequence res = repeat(0,n)
mpz num = mpz_init(1)
for i=1 to n do
mpz_mul_si(num,num,i)
if mpz_odd(num) then
mpz_sub_ui(num,num,1)
else
mpz_add_ui(num,num,1)
end if
res[i] = mpz_get_str(num)
-- res[i] = mpz_init_set(num)
end for
return res
end function
 
?extract(subfactorial(20),tagset(9)&20)</lang>
{{out}}
<pre>{"0","1","2","9","44","265","1854","14833","133496","895014631192902121"}</pre>
 
=={{header|PicoLisp}}==
7,794

edits