Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Josephus Problem
- def josephus(n, k):
- # create a list of integers: [1, 2, ... , n]
- lst = list(range(1, n+1))
- # initialize index to -1 as counting goes from index 0 to k the first time
- ind = -1
- # the loop continues until all but one element has been removed
- while len(lst) != 1:
- # compute the index of the next element to be removed and remove that element
- ind = (ind + k) % len(lst)
- lst.remove(lst[ind])
- # Every time an element is removed, the rest of the elements to the
- # right of it gets shifted one step to the left. So, the next count
- # starts from the index to the left of the last one
- ind -= 1
- # when only one element is left in the list, return that element
- return lst[0]
- N, K = map(int, input().split())
- print(josephus(N, K))
Advertisement
Add Comment
Please, Sign In to add comment