Skip to content

Commit

Permalink
Josephus Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
detel committed Jul 9, 2015
1 parent 03edca4 commit 7a5022b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Josephus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from sys import stdin

def josephus(num,k):
if num <= 1:
return num
prev = 1
for i in xrange(2,num+1):
prev = ((prev+k-1)%i+1)
return prev

while True:
N,D = map(int,stdin.readline().split())
if N == 0 and D == 0:
break
print josephus(N,D)

0 comments on commit 7a5022b

Please sign in to comment.