Help with 32 Josephus Problem

Back to Problem Solutions forum

a_banderov     2014-08-11 16:04:32

With that code the sample n = 10 and k = 3 is giving me 4. I have checked with the debugger every step and it seems that the numbers are disappearing correctly. However, when I paste the code for checking,I receive wrong answer.

What I am doing wrong? Could anyone help me?

Rodion (admin)     2014-08-11 16:37:16
User avatar

Hm... I guess there are not too many people using C#... :(

Have you tested the code with other input pairs, like 4 2? It seems the result is not quite correct, if I am not mistaken myself...

a_banderov     2014-08-11 16:48:33

With 4 2 it gives me 3. It should be 1, tough. So, my logic is wrong. On Wikipedia they are solving it very easily with recursion, but it is very hard for me to understand what is going on with the values. I will try to think of something else.

Rodion (admin)     2014-08-11 16:56:54
User avatar

I'm not sure I can easily understand the formula from Wikipedia, though I once read its proof somewhere but now I forgot of it again.

However since values are not very large the exercise could be solved with direct counting.

nicolas_patrois     2014-08-11 18:36:22
User avatar

I used the Wikipedia algorithm (please don’t tell my family).

ckoenders1990     2016-01-07 11:40:02

I used a Queue for this problem. By using Enqueue(Dequeue) in an if statement with a counter I managed to get the right results. I must say though that I don't know if it is the best approach as I am still new to programming.

Please login and solve 5 problems to be able to post at forum