I saw this video about the Josephus problem yesterday and immediately I thought it might be a fun problem to solve in Erlang. First, here is the video that got me interested.
With Recursion and Pattern Matching
I thought this would be an interesting problem to solve with Erlang’s recursion and pattern matching. I decided to use a list of integers to represent the soldiers in the circle, and a recursive function to loop around the list and remove every second integer until only one remains. Pattern matching makes it easy to pick out every second integer. Here is my first attempt at solving the problem with recursion and pattern matching:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 

1 2 3 4 5 6 7 8 9 10 11 12 

While this solution only requires 4 function clauses, it would be nice if it only required 2 clauses. Ideally we should only have one clause for when one number remains in the list, and another clause for when there are two or more numbers in the list. We actually can do this in Erlang with only two clauses by using the ++
operator to concatenate surviving numbers onto the end of the list of remaining numbers, so that it is never empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 

I usually try to avoid using ++
operator in Erlang because it creates an entirely new list each time it’s used, so while it’s more elegant than the first solution it’s less efficient. On small lists this won’t matter. The largest list I created above only contained 41 integers, so both solutions are fast enough for this.
With Binary Pattern Matching
I also thought binary method of solving the problem was interesting. All you need to do is move the first bit in the binary representation of the number to the end of the binary. This is also something that can be done easily with Erlang’s pattern matching, and since it only requires moving around a couple bits it should be by far the fastest way to solve the problem.
After a little tinkering, and little help from #erlang IRC channel, I got it working:
1 2 3 

Here I’m not actually moving bits around, I’m moving bytes, because the integer_to_binary/2
call returns a binary representing a sequence of ones and zeros, e.g. <<"101">>
. Ideally I’d like move bits instead of bytes. This is easy in Erlang, but it took me a while to figure how to how to get this to work. I asked a quite a few questions on this before I finally got it working. Note here that I’m not using Erlang’s integer_to_binary/2
function, I’m using binary:encode_unsigned/1
. encode_unsigned
converts the number to binary notation, which is the representation required for the bit moving trick to work. After we convert the number to binary notation we remove all leading zerobits. Then move the first bit to the end, and convert it back to an integer. You can see we have to specify the number of bits that should be used to construct the resulting integer. We count the number of bits in the original binary to get this number.
1 2 3 4 5 6 7 8 9 10 

Conclusion
When writing this I looked online for solutions to the Josephus problem in Erlang and found several different solutions. Some used recursive functions like I did and others used Erlang processes. Erlang’s pattern matching and recursion makes it an ideal tool for solving this sort of problem. It was a fun problem to solve, and with Erlang I didn’t have to spend any time thinking about how I was going to translate the problem into Erlang, I just jumped in and started coding. The binary solution is by far the most efficient, but all of them are expressive and relatively succinct.
References
 https://www.youtube.com/watch?v=uCsD3ZGzMgE
 https://en.wikipedia.org/wiki/Josephus_problem
 http://erlang.org/doc/efficiency_guide/myths.html#id63347
 https://rosettacode.org/wiki/Binary_digits#Erlang
 https://rosettacode.org/wiki/Josephus_problem#Erlang
 http://erlang.org/pipermail/erlangquestions/2011August/060833.html
 http://stackoverflow.com/questions/40334637/convertbase2numberinabinarytoanerlanginteger
 http://stackoverflow.com/questions/40453402/converterlangintegertopositionalnotationbinary