r/dailyprogrammer • u/Cosmologicon 2 3 • Nov 06 '12
[11/6/2012] Challenge #111 [Difficult] The Josephus Problem
Flavius Josephus was a roman historian of Jewish origin. During the Jewish-Roman wars of the first century AD, he was in a cave with fellow soldiers, 41 men in all, surrounded by enemy Roman troops. They decided to commit suicide by standing in a ring and counting off each third man. Each man so designated was to commit suicide. (When the count came back around the ring, soldiers who had already committed suicide were skipped in the counting.) Josephus, not wanting to die, placed himself in position #31, which was the last position to be chosen.
In the general version of the problem, there are n soldiers numbered from 1 to n and each k-th soldier will be eliminated. The count starts from the first soldier. Write a function to find, given n and k, the number of the last survivor. For example, josephus(41, 3) = 31. Find josephus(123456789101112, 13).
Thanks to user zebuilin for suggesting this problem in /r/dailyprogrammer_ideas!
14
u/Ledrug 0 2 Nov 06 '12
C:
output:
6
u/more_exercise Nov 07 '12
I can't be the only one that thinks that there's a constant-time solution to this.
3
u/skeeto -9 8 Nov 06 '12
Common Lisp. I shamelessly stole Ledrug's algorithm since I couldn't figure out the
k log nalgorithm on my own, but I took the time to make it recursive.The output is the same:
3
u/Wriiight Nov 08 '12 edited Nov 08 '12
At first I had this as a for loop, single iteration at a time. Took all day and never returned on the big number. Took me a full day to figure out how simple a change would make it efficient. At one point made an excel version that worked a completely different way.
I think it is essentially the same algorithm as Ledrug, independently derived and better commented. ;)
3
u/beefheart_ 0 0 Nov 16 '12 edited Nov 16 '12
here's a recursive approach in java:
3
u/thehivemind5 Dec 24 '12
Just discovered this sub-reddit and going through the older problems
Java solution, handles both test cases in under a second, same output as above posters.
5
u/gammadistribution 0 0 Nov 06 '12
Awesome! A combinatorics problem! Graham, Knuth, and Patashnik would be proud.
2
u/njchessboy Nov 07 '12 edited Nov 07 '12
My recursive Python3 solution:
Edit: Yes, I'm aware it takes forever on josephus(123456789101112, 13)
2
u/prophile Nov 07 '12
Haskell solution:
2
u/[deleted] Nov 07 '12 edited Nov 08 '12
python:
I have no idea how to do it for the large number I get an OverflowError
1
u/je4d Nov 07 '12
First solution I worked out that wasn't horribly slow.
It's not as efficient in absolute terms as Ledrug's, but I think it's still O(k log n). It does josephus(123456789101112, 13) without any noticeable delay, and with the same result.
Its recursion depth for (123456789101112, 13) is 392, which is about what I'd expect as n is reduced by a factor of ((k-1)/k) on each recursive call, and (log(123456789101112) / log(13/12)) = ~405
1
u/Tryer1234 Nov 10 '12
Can someone please help me with my python-based solution? Bare in mind I am not on the level of this problem, and should probably be attempting the easy instead of difficult problems, I also have not learned log in school yet so I tried to devise an alt solution. It usually gets close, but never gets the exact number. Thanks!
1
u/I_had_to_know_too Nov 16 '12
Spent some time writing this with an array of soldiers (ints) that were alive (1) or dead (0), and my Josephus function ran along killing and traversing.
It worked fine for 41 soldiers, but I couldn't allocate an array of 123456789101112 soldiers, and even with 123456789 soldiers, it took too long to wait...
So I took a peek at Ledrug's solution - I don't understand it at all...
I guess I'll leave the difficult challenges alone for a while.
0
u/[deleted] Nov 30 '12
[deleted] — view deleted comment
0
u/[deleted] Nov 06 '12
[deleted] — view deleted comment