Combinations and permutations: Difference between revisions
Content deleted Content added
Nimrod -> Nim |
→{{header|Julia}}: A new entry for Julia |
||
Line 524: | Line 524: | ||
60 | big_combination(53) #=> 386206920.0000046 |
60 | big_combination(53) #=> 386206920.0000046 |
||
145 | big_permutation(133) #=> 1. |
145 | big_permutation(133) #=> 1.6801459655817e+243 |
||
170 | big_permutation(133) #=> 5.272846415658284e+263 |
170 | big_permutation(133) #=> 5.272846415658284e+263 |
||
=={{header|Julia}}== |
|||
Infix operators are interpreted by Julia's parser, and (to my knowledge) it isn't possible to define arbitrary characters as such operators. However one can define Unicode "Symbol, Math" characters as infix operators. This solution uses ⊞ for combinations and ⊠ for permutations. An alternative to using the <tt>FloatingPoint</tt> versions of these operators for large numbers would be to use arbitrary precision integers, <tt>BigInt</tt>. |
|||
'''Functions''' |
|||
<lang Julia> |
|||
function Base.binomial{T<:FloatingPoint}(n::T, k::T) |
|||
exp(lfact(n) - lfact(n - k) - lfact(k)) |
|||
end |
|||
function Base.factorial{T<:FloatingPoint}(n::T, k::T) |
|||
exp(lfact(n) - lfact(k)) |
|||
end |
|||
⊞{T<:Real}(n::T, k::T) = binomial(n, k) |
|||
⊠{T<:Real}(n::T, k::T) = factorial(n, n-k) |
|||
</lang> |
|||
'''Main''' |
|||
<lang Julia> |
|||
function picknk{T<:Integer}(lo::T, hi::T) |
|||
n = rand(lo:hi) |
|||
k = rand(1:n) |
|||
return (n, k) |
|||
end |
|||
nsamp = 10 |
|||
print("Tests of the combinations (⊞) and permutations (⊠) operators for ") |
|||
println("integer values.") |
|||
println() |
|||
lo, hi = 1, 12 |
|||
print(nsamp, " samples of n ⊠ k with n in [", lo, ", ", hi, "] ") |
|||
println("and k in [1, n].") |
|||
for i in 1:nsamp |
|||
(n, k) = picknk(lo, hi) |
|||
println(@sprintf " %2d ⊠ %2d = %18d" n k n ⊠ k) |
|||
end |
|||
lo, hi = 10, 60 |
|||
println() |
|||
print(nsamp, " samples of n ⊞ k with n in [", lo, ", ", hi, "] ") |
|||
println("and k in [1, n].") |
|||
for i in 1:nsamp |
|||
(n, k) = picknk(lo, hi) |
|||
println(@sprintf " %2d ⊞ %2d = %18d" n k n ⊞ k) |
|||
end |
|||
println() |
|||
print("Tests of the combinations (⊞) and permutations (⊠) operators for ") |
|||
println("(big) float values.") |
|||
println() |
|||
lo, hi = 5, 15000 |
|||
print(nsamp, " samples of n ⊠ k with n in [", lo, ", ", hi, "] ") |
|||
println("and k in [1, n].") |
|||
for i in 1:nsamp |
|||
(n, k) = picknk(lo, hi) |
|||
n = BigFloat(n) |
|||
k = BigFloat(k) |
|||
println(@sprintf " %7.1f ⊠ %7.1f = %10.6e" n k n ⊠ k) |
|||
end |
|||
lo, hi = 100, 1000 |
|||
println() |
|||
print(nsamp, " samples of n ⊞ k with n in [", lo, ", ", hi, "] ") |
|||
println("and k in [1, n].") |
|||
for i in 1:nsamp |
|||
(n, k) = picknk(lo, hi) |
|||
n = BigFloat(n) |
|||
k = BigFloat(k) |
|||
println(@sprintf " %7.1f ⊞ %7.1f = %10.6e" n k n ⊞ k) |
|||
end |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Tests of the combinations (⊞) and permutations (⊠) operators for integer values. |
|||
10 samples of n ⊠ k with n in [1, 12] and k in [1, n]. |
|||
4 ⊠ 2 = 12 |
|||
9 ⊠ 2 = 72 |
|||
2 ⊠ 1 = 2 |
|||
8 ⊠ 2 = 56 |
|||
7 ⊠ 5 = 2520 |
|||
4 ⊠ 2 = 12 |
|||
9 ⊠ 8 = 362880 |
|||
11 ⊠ 6 = 332640 |
|||
1 ⊠ 1 = 1 |
|||
8 ⊠ 5 = 6720 |
|||
10 samples of n ⊞ k with n in [10, 60] and k in [1, n]. |
|||
58 ⊞ 26 = 22150361247847371 |
|||
22 ⊞ 21 = 22 |
|||
32 ⊞ 30 = 496 |
|||
11 ⊞ 4 = 330 |
|||
32 ⊞ 29 = 4960 |
|||
16 ⊞ 7 = 11440 |
|||
31 ⊞ 25 = 736281 |
|||
13 ⊞ 13 = 1 |
|||
43 ⊞ 28 = 151532656696 |
|||
49 ⊞ 37 = 92263734836 |
|||
Tests of the combinations (⊞) and permutations (⊠) operators for (big) float values. |
|||
10 samples of n ⊠ k with n in [5, 15000] and k in [1, n]. |
|||
8375.0 ⊠ 5578.0 = 2.200496e+20792 |
|||
1556.0 ⊠ 592.0 = 1.313059e+1833 |
|||
1234.0 ⊠ 910.0 = 2.231762e+2606 |
|||
12531.0 ⊠ 9361.0 = 2.339542e+36188 |
|||
12418.0 ⊠ 6119.0 = 1.049662e+24251 |
|||
9435.0 ⊠ 8960.0 = 4.273644e+32339 |
|||
9430.0 ⊠ 5385.0 = 1.471741e+20551 |
|||
9876.0 ⊠ 5386.0 = 9.073417e+20712 |
|||
941.0 ⊠ 911.0 = 8.689430e+2358 |
|||
8145.0 ⊠ 4357.0 = 1.368129e+16407 |
|||
10 samples of n ⊞ k with n in [100, 1000] and k in [1, n]. |
|||
757.0 ⊞ 237.0 = 6.813837e+202 |
|||
457.0 ⊞ 413.0 = 4.816707e+61 |
|||
493.0 ⊞ 372.0 = 8.607443e+117 |
|||
206.0 ⊞ 26.0 = 6.911828e+32 |
|||
432.0 ⊞ 300.0 = 1.248351e+114 |
|||
650.0 ⊞ 469.0 = 3.284854e+165 |
|||
203.0 ⊞ 115.0 = 1.198089e+59 |
|||
583.0 ⊞ 429.0 = 5.700279e+144 |
|||
329.0 ⊞ 34.0 = 2.225630e+46 |
|||
464.0 ⊞ 178.0 = 5.615925e+132 |
|||
</pre> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |