5 Computer -- Recent Trends in ICT

ask mattrab Visit www.askmattrab.com for more academic resources.

Josephus Problem

NOTE: The following program is written in python.

 

Ever heard about Josephus problem, if not then here's a brief description: 

Josephus Problem

The Problem:

People are standing in a circle, waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and is freed.

 

Solution:


no_of_people = int(input("What is the number of people ? "))

list_people = []
for number in range(1,no_of_people+1):
list_people.append(number)

while len(list_people) > 1 :
i = 0
while i < len(list_people):
try:
list_people.pop(i+1)
i += 1
except IndexError:
list_people.pop(0)
else:
print('The correct or safe seating position is : '+ str(list_people[0]))


Discussions

More notes on Recent Trends in ICT

Close Open App