- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Interesting pattern when you line up the plotted points of the "safe spot" in varying numbers of soldiers...
Admin
Here is my C# 3.5 solution. I kept looking for a nice easy few liner for it but ran out of time.
Admin
$ cat saveJos.c
#include <stArmyFunctions.h>
int main (int argc, const char **argv) { if(argc > 0) { DefectFromArmy(); //from stArmyFunctions fprintf ("Josephus successfully defects from the army and survives!"); } else fprintf ("Josephus has nothing to worry about as they are killing nobody"); return 0; }
Admin
Here's a XSLT solution - with graphics! Needs XSLT 2.0 and uses EXSLT for the graphics (which aren't really necessary), so it probably will not run on your browser. Here's a sample Saxon output.
Takes three parameters - the number of soldiers, the number of people to skip between two victims and the radius of the graphical circles in pixels.
Since the output is done with pixel positioning, your mileage might vary on different operating systems and browsers. And I know that the "copyright" text might be under the circles.
Example XML:
And here's the XSLT:
Addendum (2009-07-31 02:21): In light of the other XSLT solution, I might add that this is the brute-force solution. :)
Admin
Admin
Brute Force C#
Admin
I haven't read through all the comments, but a search for Brainfuck on each page of comments didn't return anything, so I decided to give it a go.
I haven't optimised the final code - I took Alex Mont's Python code, expanded the recursivity to a simple for-loop and proceeded to hack my way through in BF.
Original Python:
Port to C without recursivity:
Finally, in Brainfuck: (result in memory cell 0, input in cells 1 and 2 (no. of men, kill-every)).
I couldn't find an atoi in Brainfuck, so I added the ordinal of ascii zero (48) and output, so it'll break if the result is >9. (for the reader's convenience, the sequence is 0123456789:;<=>?@ABC...)
Admin
The trick to staying alive isn't knowing the safe spot... it's being the man with the axe.
Admin
is to be A LUMBERJACK
Admin
A somewhat juvenile Perl solution.
Addendum (2009-07-31 00:51): I should note. The program takes two arguments on the commandline - the first is the number of men, and the second is the number to skip each time. There's no checking on those, so if you give zero men, you'll get a divide by zero error (don't ask) and if you give zero skip size you'll get an infinite loop.
It prints out a zero-indexed solution.
Admin
A really random solution in Java.
Admin
Haskell
Admin
C# implementation:
Admin
This is officially awesome. I thought of making a solution in Piet, but thought it'd be too tricky. It's good to be proven wrong, though.
Admin
Bah, the rules are dizzy! Count of three means that I start to count at 1 and number 3 gets killed. In some examples you kill number 2 because you start to count at zero. Don't forget that in those times they did not have arrays yet and people started to count at 1. What I found particularly difficult was the "hop over the edge", alas what do you do when 10 are in the array and you need to count to 11?
#!/usr/bin/perl -w strict my $group=$ARGV[0]; my $count=$ARGV[1]; print "$group people count to $count\n";
Create an array
my @circle; my $array_length; for ($i = 1 ; $i < $group+1 ; $i++) { $array_length=push (@circle,$i); } print "Starting circle: @circle\n"; my $victim=0; while (@circle > 1) { my $loop=0; my $nextvictim=($victim)+$count-1; while ($nextvictim >= @circle) {$nextvictim=$nextvictim-@circle;$loop++;} $victim=$nextvictim; print "Remove $victim ($circle[$victim]) from @circle "; if ($loop > 0) {print" (looped $loop times)"}; print "\n"; splice(@circle,$victim,1); } print "Number @circle survives\n";
for 10/3 that gives me ./titus.pl 10 3 10 people count to 3 Starting circle: 1 2 3 4 5 6 7 8 9 10 Remove 2 (3) from 1 2 3 4 5 6 7 8 9 10 Remove 4 (6) from 1 2 4 5 6 7 8 9 10 Remove 6 (9) from 1 2 4 5 7 8 9 10 Remove 1 (2) from 1 2 4 5 7 8 10 (looped 1 times) Remove 3 (7) from 1 4 5 7 8 10 Remove 0 (1) from 1 4 5 8 10 (looped 1 times) Remove 2 (8) from 4 5 8 10 Remove 1 (5) from 4 5 10 (looped 1 times) Remove 1 (10) from 4 10 (looped 1 times) Number 4 survives
and for 40/3 40 people count to 3 Starting circle: 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 31 32 33 34 35 36 37 38 39 40 Remove 2 (3) from 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 31 32 33 34 35 36 37 38 39 40 .. Remove 2 (32) from 13 28 32 Remove 0 (13) from 13 28 (looped 2 times) Number 28 survives
I really don't know if that's correct as I only checked manually for small numbers. I tested though what happens if you put high numbers (40 people kill every 1.000.000 person: Stand in position 36)
And now I will read the wikipedia article ;-)
Admin
public static int Josephus(int number, int skip) { return number == 1 ? 0 : (Josephus(number - 1, skip) + skip) % number; }
Admin
Admin
If you want to be REALLY FAST (in your head), here's an algorithm
When you know a last spot for a given number if soldiers, Add 3 to the spot if you add one soldier. If your number is above the number of soldiers, just reduce it (ie: remove by the amount of people in the circle)
ie: For 20 soldiers, the spot is 20 (easy to remember) Add 3, For 21 soldiers it should be 23, so you are over, substract 21 and you get 2 as the spot. Add 3, For 22 soldiers the spot is 5 Add 3, For 23 soldiers the spot is 8 ... Add 3, For Soldiers: 30 the spot is 29 Add 3, For 31 soldiers it should be 32, so you are over, substract 31 and you get 1 as the spot. ...
Admin
Here's dc code implementing the 'obvious' algorithm (fill a stack with soldiers and delete them one by one:
Admin
Well, here's my try. I'm late to join the party, I know. But its friday night, and I had to do something while getting drunk for the pub. ;) And I figured that making a program in python for the first time would be a good way to spend the time.
If someone feels like it, point out the bad stuff. Heh.
Admin
I disagree with using a singleton pattern here... after all, the story is set in Roman times.
God.get("Mars").sortOut(soldiers);
There, I corrected it for you.
Admin
When I was a kid, I could actually pick the last spot in circles to be singled out in these games. I cant remember how I did it, but it wasnt overly complicated. I think I counted the size of the circle up backwards from two, two numbers each step and then fixing at the end if it was an odd number of people.
Admin
Never; saw; so; many; semicolons; in; a; Python; program; :-) But if it's your first Python program that's OK.
Admin
Haha, being tipsy prolly didnt help me much either. >.<
Admin
...no, that gives the wrong answer.
Admin
Soldiers to Skip:<input name="number2" type="text" length="5">
'; $body .= '<input type="submit" action="submit" value="Calculate"></form></body></html>'; echo $body; } else { $amount = $_GET['number1']; $skip = $_GET['number2']; $soldiers = array_flip(range(1, $amount)); for($deathblow = 1; $soldiers[$deathblow] != end($soldiers); $deathblow++) { if((($deathblow + $skip) % $skip) != 0) array_push($soldiers, $soldiers[$deathblow]); } echo "Stand in spot ".(end($soldiers) + 1)." if you want to live!"; } ?>
Admin
Ok, I think you nailed it... Takes a few seconds, doesn't require any knowledge other than adding and subtracting, it's a great solution! Kudos, Stéphane! You win! :)
Admin
The goal of this is not to be clever, but to provide a solution in Perl that won't be recognized as being Perl. Here goes:
Invoke as
Admin
Man,where is the Haskell love? Here's a quick hack.
praxis :: Int -> Int -> Int praxis 1 k = 0 praxis n k = mod ((praxis (n-1) k)+k) n
coward:: Int-> Int -> IO () coward n k = do c <- return (show ((praxis n k)+1)) putStrLn ("With "++show n++" soldiers and "++show k++"soldiers to skip, stay at position "++c)
Addendum (2009-08-02 15:23): This is based on the previous Python solution.
Addendum (2009-08-02 15:24): by Alex Mont.
Admin
javascript.
Admin
Admin
Minski register machine: [image] Basic model of computation: you have named random-access registers and an FSM. An (x++) node increments register x and moves to its single successor. An (x--) node first checks whether register x is 0: if so, it moves to the double-arrow successor; otherwise it decrements register x and moves to the single-arrow successor.
This machine takes input in n (number of soldiers) and k (advance) and gives output in r (zero-indexed). It is assumed that r, i, and t are all 0 before computation begins.
The first two HALT nodes are error conditions (respectively k=0 and n=0).
Addendum (2009-08-01 07:35): s/Minski/Minsky/ My apologies.
Admin
When, in fact, god is a simpleton. ;)
Admin
Assuming you only believe in one God... unfortunately this problem has been outsourced to India so this code is no good.
Admin
It's sad, the people who simply looked it up on Wikipedia or elsewhere didn't even bother to change the variable names.
I don't think this is an indication of the need for a "more unique problem to solve." I think it's just a sign that there are many, many losers who value posting the "right" solution first over actually solving the problem on their own.
Admin
Several people were talking about how you could work this out in your head. Well I was thinking about this last night in bed and here is what I was able to come up with:
In your mind split the 41 people up into rows containing 9 soldiers each. This will give you 4 rows of 9 and one row at the end with 5.
One the first round of killing you will lose columns 3, 6 and 9. Two soldiers remain after the last killing (columns 4 and 5) so on the second run through columns 1 and 5 will be taken out. This leaves you with columns 2478 (remember this number!) and 18 soldiers still alive.
Again divide these soliders into 9 columns (which gives you exactly two rows). On the previous run we killed off column 5 last so we will lose columns 3, 6 and 9 again, followed by 4 and 8. This leaves columns 1257 (remember this number) and 8 soldiers still alive.
It is trivial at this stage to work out that the safe spot out of these 8 is position 7 (you may be able to do this visually in your mind, or if you have to, resort to using fingers. Remember the first to die is the 3rd one as we killed off the last guy in the previous round).
We know that the safe position is number 7, but this is from the final 8 soldiers. We need to move back up the grids and work out what the real position is. Thankfully this is easy. The number of surviving columns each time was 4, so see how many 4s you can get into 7, this tells you how many rows to skip, then the remainder is the index of the safe column (from the numbers you remembered).
So:
7 = 4 + 3 Down one row, 3rd safe column (from 1257) so column 5.
9 soldiers per row so safe position is (9 * 1) + 5 = 14
14 = 4 + 4 + 4 + 2 Down three rows, 2nd safe column (from 2478) so column 4 9 soldiers per row so safe position is (9 * 3) + 4 = 31
So there is your final answer, you need to be standing in position 31. And all you had to remember was two four digit numbers and who was first to die between stages.
Admin
(It's also being used for language one-upmanship, but that's just the TDWTF crowd for you.)
Admin
This is what the singleton is meant for.
If you have code all over your program like this:
god = God.getInstance(); god.sortOut(theDead); god.createHeavens(); // etc.
Then you only have to update 1 line of code rather than all of them, like you would with a static class.
God.sortOut(theDead); God.createHeavens(); // etc.
Admin
I always remember being told that you would feel more connected with things if you named them, so I made sure each soldier had a name (I have both Jewish and Roman name lists now, number 31 is Josephus in both.)
Example output:
Admin
Part of being a good programmer is to know that you are not the best at everything and finding solutions done by someone better at it than you. And then implementing it in code.
The only thing wrong I see with digging the solution up on wikipedia is that there were no credits. I think you should always credit the guy or place you got the solution from.
Admin
My C# implementation, with the aformentioned recursion, but with as much /obfustication/ that if you read it right is a little poem to the reader.
and no, there are no unnecesary spaces within this.
I'm a little rusty on my lisp but I think the LISP version would be:
... Just my two cents
Admin
Brute C# solution
Admin
Admin
Your socks and shirts are ful of God?
Admin
Some perl:
Admin
If someone hasn't figured it out, yet, there is a simple pattern to it, if you read the list at http://thedailywtf.com/Comments/Programming-Praxis-Josephus-Circle.aspx#280029
you can see that with each increasing number of soldiers, the safe spot rises once by the kill factor. if the number is bigger than the current amount of people, simply continue from the bottom, again.
quick C# Implementation:
public static uint Josephus(uint n, uint k) { uint safespot = 1;
}
Also, this will still need a bit of brainpower to do in the head, but better than any modulo or recursive calls out there.
Admin
Damn. Forgot BBCodes, also forgot to set 3 to k. fix:
Admin
most of the solutions are wtfs...
josephus needs to be the 40th man
Admin
Quick Python solution:
Admin
The story is about the Jews who do not follow the Roman gods, but do follow a Singleton God.