(* ************************************************************ *
 *								*	
 *	      	       Josephus Problem				*
 *								*
 * Knuth's Exercise 22; Section 1.3.2 in Fundamental Algorithms	*
 *								*
 * ************************************************************ *)


		MIXAL

	24	EQU	N
	11	EQU	M
| X		ORIG	* N +

| 0H		ENT1	N 1-		\ 1    Set each cell to
		STZ	X N + 1-	\ 1    	   number of next man	
		ST1	X 1-,1		\ N-1      in the sequence
		DEC1	1		\ N-1 
		J1P	* 2-		\ N-1 
		ENTA	1		\ 1 	(now rI1=0)
| 1H		ENT2	M 2-		\ N-1   (assume M > 2)
		LD1	X,1		\ (M-2)(N-1)  Count around 
		DEC2	1		\ (M-2)(N-1)    the circle.
		J2P	* 2-		\ (M-2)(N-1)
		LD2	X,1		\ N-1	rI1 = lucky man
		LD3	X,2		\ N-1   rI2 = doomed man
		CHAR			\ N-1   rI3 = next man
		STX	X,2(4:5)	\ N-1   store execution number
		NUM			\ N-1
		INCA	1		\ N-1
		ST3	X,1		\ N-1   Take man from circle.
		ENT1	0,3		\ N-1	
		CMPA	=N=		\ N-1
		JL	1B		\ N-1
		CHAR			\ 1 	One man left
		STX	X,1(4:5)	\ 1	(he is clobbered too).
		OUT	X(18)		\ 1	Print answer.
		HLT			\ 1
		END	0B

\ The last man is in position 15. The total time before output is 
\ (4N-1)(M+3)+7 u. 
\ Improvements noted by Knuth are too horrible to mention.

			        (* End of File *)