Skip to content

Commit d8bc4ef

Browse files
authored
Josephus_problem.py
Josephus_Problem
1 parent 3a8938d commit d8bc4ef

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Josephus_problem.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#Josephus Problem
2+
3+
# The Josephus problem, where 100 people standing in a circle, 1 kills 2 passes the sword to 3….who survives in the end?
4+
5+
#Python Solution
6+
7+
from sys import stdin,stdout
8+
9+
def Josephus_Prblm(n, k):
10+
if (n == 1):
11+
return 1
12+
else:
13+
return ((Josephus_Prblm(n - 1, k) + k-1) % n + 1)
14+
15+
for _ in range(int(stdin.readline())):
16+
N = int(stdin.readline())
17+
K = int(stdin.readline())
18+
print(Josephus_Prblm(N,K)
19+
20+

0 commit comments

Comments
 (0)