September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,24 @@
|
|||
def derangements:
|
||||
|
||||
# In order to reference the original array conveniently, define _derangements(ary):
|
||||
def _derangements(ary):
|
||||
# We cannot put the i-th element in the i-th place:
|
||||
def deranged: # state: [i, available]
|
||||
.[0] as $i | .[1] as $in
|
||||
| if $i == (ary|length) then []
|
||||
else
|
||||
($in[] | select (. != ary[$i])) as $j
|
||||
| [$j] + ([$i+1, ($in - [$j])] | deranged)
|
||||
end
|
||||
;
|
||||
[0,ary]|deranged;
|
||||
. as $in | _derangements($in);
|
||||
|
||||
def subfact:
|
||||
if . == 0 then 1
|
||||
elif . == 1 then 0
|
||||
else (.-1) * (((.-1)|subfact) + ((.-2)|subfact))
|
||||
end;
|
||||
|
||||
# Avoid creating an array just to count the items in a stream:
|
||||
def count(g): reduce g as $i (0; . + 1);
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
"Derangements:",
|
||||
([range(1;5)] | derangements),
|
||||
"",
|
||||
"Counted vs Computed Derangments:",
|
||||
(range(1;10) as $i | "\($i): \(count( [range(0;$i)] | derangements)) vs \($i|subfact)"),
|
||||
"",
|
||||
"Computed approximation to !20 (15 significant digits): \(20|subfact)"
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
$ jq -n -c -r -f derangements.jq
|
||||
jq -n -c -r -f derangements.jq
|
||||
|
||||
Derangements:
|
||||
[2,1,4,3]
|
||||
[2,3,4,1]
|
||||
[2,4,1,3]
|
||||
[3,1,4,2]
|
||||
[3,4,1,2]
|
||||
[3,4,2,1]
|
||||
[4,1,2,3]
|
||||
[4,3,1,2]
|
||||
[4,3,2,1]
|
||||
|
||||
Counted vs Computed Derangments:
|
||||
1: 0 vs 0
|
||||
2: 1 vs 1
|
||||
3: 2 vs 2
|
||||
4: 9 vs 9
|
||||
5: 44 vs 44
|
||||
6: 265 vs 265
|
||||
7: 1854 vs 1854
|
||||
8: 14833 vs 14833
|
||||
9: 133496 vs 133496
|
||||
|
||||
Computed approximation to !20 (15 significant digits): 895014631192902000
|
||||
Loading…
Add table
Add a link
Reference in a new issue