Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
20
Task/Josephus-problem/Nim/josephus-problem-1.nim
Normal file
20
Task/Josephus-problem/Nim/josephus-problem-1.nim
Normal 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)
|
||||
10
Task/Josephus-problem/Nim/josephus-problem-2.nim
Normal file
10
Task/Josephus-problem/Nim/josephus-problem-2.nim
Normal 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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue