aboutsummaryrefslogtreecommitdiff
path: root/challenge-048/archargelod/nim/ch_1.nim
blob: f04fe841ab646713fbe1ede5298330661d1a8b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off
import std/bitops

proc highestOneBit(n: int): int =
  if n == 0: return 0
  1 shl (sizeof(n) * 8 - countLeadingZeroBits(n) - 1)

proc josephusSurvivor(n: int): int =
  not(highestOneBit(n*2)) and ((n shl 1) or 1)
when isMainModule:
  import std/unittest

  const
    Test = 50
    Expected = 37

  suite "Survivor":
    test "50 people, k = 2":
      check josephusSurvivor(Test) == Expected