Skip to content

Commit 7a5022b

Browse files
committed
Josephus Problem
1 parent 03edca4 commit 7a5022b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Josephus.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from sys import stdin
2+
3+
def josephus(num,k):
4+
if num <= 1:
5+
return num
6+
prev = 1
7+
for i in xrange(2,num+1):
8+
prev = ((prev+k-1)%i+1)
9+
return prev
10+
11+
while True:
12+
N,D = map(int,stdin.readline().split())
13+
if N == 0 and D == 0:
14+
break
15+
print josephus(N,D)

0 commit comments

Comments
 (0)