Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,20 @@
import sequtils, strutils, sugar
proc j(n, k: int): string =
var
p = toSeq(0 ..< n)
i = 0
s = newSeq[int]()
while p.len > 0:
i = (i + k - 1) mod p.len
s.add p[i]
system.delete(p, i)
result = "Prisoner killing order: "
result.add s.map((x: int) => $x).join(", ")
result.add ".\nSurvivor: "
result.add($s[s.high])
echo j(5,2)
echo j(41,3)

View file

@ -0,0 +1,10 @@
func prisonerPos(n, k: Positive): int =
## The result is computed backwards. We start from the winner at
## position 0 on last round and compute its position on previous rounds.
var pos = 0
for i in 2..n:
pos = (pos + k) mod i
result = pos
echo "Survivor: ", prisonerPos(5, 2)
echo "Survivor: ", prisonerPos(41, 3)