Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
12
Task/Josephus-problem/Jq/josephus-problem-1.jq
Normal file
12
Task/Josephus-problem/Jq/josephus-problem-1.jq
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# A control structure, for convenience:
|
||||
# as soon as "condition" is true, then emit . and stop:
|
||||
def do_until(condition; next):
|
||||
def u: if condition then . else (next|u) end;
|
||||
u;
|
||||
|
||||
# n is the initial number; every k-th prisoner is removed until m remain.
|
||||
# Solution by simulation
|
||||
def josephus(n;k;m):
|
||||
reduce range(0;n) as $i ([]; . + [$i]) # Number the prisoners from 0 to (n-1)
|
||||
| do_until( length < k or length <= m; .[k:] + .[0:k-1] )
|
||||
| do_until( length <= m; (k % length) as $i | .[$i:] + .[0:$i-1] );
|
||||
5
Task/Josephus-problem/Jq/josephus-problem-2.jq
Normal file
5
Task/Josephus-problem/Jq/josephus-problem-2.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def task(n;k;m):
|
||||
"Survivors for n=\(n), k=\(k), m=\(m): \( josephus(n;k;m) )";
|
||||
|
||||
task(41;3;1),
|
||||
task(23482; 3343; 3)
|
||||
Loading…
Add table
Add a link
Reference in a new issue