Bad Perl: Josephus problem

Bad Perl: Josephus problem

  • Comments 1

Another programming contest asks to solve the Josephus problem.

Bad Perl solution (83 characters... so close...)

>perl -e"@_=(1..$ARGV[0]);++$c%$ARGV[1]?$i++:splice@_,$i%=@_,1while$#_;print@_" 40 3
28

EDIT: got it down to 80.

>perl -e"@_=(1..shift);++$c%$ARGV[0]?$i++:splice@_,$i%=@_,1while$#_;print@_" 40 3
28

EDIT2: 78 dropping the parentheses.

>perl -e"@_=1..shift;++$c%$ARGV[0]?$i++:splice@_,$i%=@_,1while$#_;print@_" 40 3
28

EDIT3: 66, shamelessly cannibalizing others' ideas from the contest (though I refuse to use "say")

>perl -e"$k=pop;@_=1..pop;@_=grep{++$i%$k}@_ while$#_;print@_" 40 3
28

Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post
  • 76 I believe (wc -m says so

    perl -e '@A=1..$ARGV[0];while($A[1]){$j=shift@A;push(@A,$j)if(++$i)%$ARGV[1]}print@A' 12 3

    Pat

Page 1 of 1 (1 items)