SharedJosephus.ipynbOpen in CoCalc
Author: David Hart
Views : 20
Description: Jupyter notebook Josephus.ipynb

This is my coding up of the Josephus problem. I am practicing juypter scripts for my digital signal processing class. -Grasshopper

In [7]:
import time def main(args): n = 30 # Number of Men sleep_time = 0.5 men = range(1,n+1) # Calculate Largest Power of 2 a = 0 for i in range(1, n+1): if (2**i > n): break a = i k = n - 2**a predict = 2*k + 1 print "Starting Set:", men print "Prediction:", predict, "survives" time.sleep(1) print "Begin Killing..." time.sleep(0.5) for i in range(0,n-1): spot = 0 killspot = spot+1 print men[spot], "kills", men[killspot] del men[killspot] # Move the killer to the back of the line men.append(men[spot]) del men[spot] time.sleep(sleep_time) print men[0], "survives!" return 0 if __name__ == '__main__': import sys main(sys.argv)
Starting Set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30] Prediction: 29 survives Begin Killing... 1 kills 2 3 kills 4 5 kills 6 7 kills 8 9 kills 10 11 kills 12 13 kills 14 15 kills 16 17 kills 18 19 kills 20 21 kills 22 23 kills 24 25 kills 26 27 kills 28 29 kills 30 1 kills 3 5 kills 7 9 kills 11 13 kills 15 17 kills 19 21 kills 23 25 kills 27 29 kills 1 5 kills 9 13 kills 17 21 kills 25 29 kills 5 13 kills 21 29 kills 13 29 survives!
In [ ]: