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
|
|
@ -0,0 +1,19 @@
|
|||
(lib 'list) ;; in-permutations
|
||||
(lib 'bigint)
|
||||
|
||||
;; generates derangements by filtering out permutations
|
||||
(define (derangement? nums) ;; predicate
|
||||
(for/and ((n nums) (i (length nums))) (!= n i)))
|
||||
|
||||
(define (derangements n)
|
||||
(for/list ((p (in-permutations n))) #:when (derangement? p) p))
|
||||
|
||||
(define (count-derangements n)
|
||||
(for/sum ((p (in-permutations n))) #:when (derangement? p) 1))
|
||||
|
||||
;;
|
||||
;; !n = (n - 1) (!(n-1) + !(n-2))
|
||||
|
||||
(define (!n n)
|
||||
(* (1- n) (+ (!n (1- n)) (!n (- n 2)))))
|
||||
(remember '!n #(1 0))
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
(derangements 4)
|
||||
→ ((3 0 1 2) (2 0 3 1) (2 3 0 1) (3 2 0 1) (3 2 1 0) (2 3 1 0) (1 2 3 0) (1 3 0 2) (1 0 3 2))
|
||||
|
||||
;; generated versus computed
|
||||
|
||||
(for ((i 10)) (writeln i '| (count-derangements i) (!n i)))
|
||||
|
||||
0 | 1 1
|
||||
1 | 0 0
|
||||
2 | 1 1
|
||||
3 | 2 2
|
||||
4 | 9 9
|
||||
5 | 44 44
|
||||
6 | 265 265
|
||||
7 | 1854 1854
|
||||
8 | 14833 14833
|
||||
9 | 133496 133496
|
||||
|
||||
(!n 20)
|
||||
→ 895014631192902121
|
||||
|
|
@ -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