David had the right answer of 37 as the best position to survive when every 4th prisoner is killed in a group of 38. Here's a new version now that I understand the problem better.
jolla=: dyad define K=:i.y while. 1<$K do. K=:1}.(<:x)|.K end. K ) 3 jolla 41 30 4 jolla 38 37 I still think there is a "one liner" in its future, but I'm not too good at that yet. http://rosettacode.org/wiki/Josephus_problem#J Linda -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Linda Alvord Sent: Thursday, April 25, 2013 12:54 PM To: [email protected] Subject: Re: [Jprogramming] rosettacode If there are 38 people in a line and every 4th on is killed, where would you stand? josephus =: dyad define NB. explicit form, assume executioner starts at position 0 NB. use: SKIP josephus NUMBER_OF_PRISONERS N =: y K =: N | x EXECUTIONER =: 0 PRISONERS =: i. N kill =: ] #~ (~: ([: i. #)) while. 1 (< #) PRISONERS do. EXECUTIONER =: (# PRISONERS) | <: K + EXECUTIONER PRISONERS =: EXECUTIONER kill PRISONERS end. ) 3 josephus 41 30 4 josephus 38 37 jolla=: dyad define B=:_1}"1 ((>.y%x) ,x)$i.y G=:(-.2|y)|.(-.(i.y) e. B)#i.y while. y>$B do. B=:B,_1{"1((>.($G)%x) ,x)$G G=:(-.(i.y) e. B)#i.y end. _1}B ) 3 jolla 41 30 4 jolla 38 18 David Lambert's function chooses the 37th position. Mine chooses the e. Where would you stand? Linda -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of David Ward Lambert Sent: Thursday, April 18, 2013 11:20 PM To: programming Subject: [Jprogramming] rosettacode Please modify these new entries as you please: http://rosettacode.org/wiki/Josephus_problem#J http://rosettacode.org/wiki/Continued_fraction/Arithmetic/Construct_from_rat ional_number#J The continued fraction solution has some oddities. Returning _ to signal "finished" converts the output vector to floating point. That's why I excluded the 314285714 100000000x example. The `y is 8' input to r2cf__CF is arbitrary and I have to strip it with _ from the output vector. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
