Monday, 30 May 2016

josephus problem

Today I came to know very interesting prblem.
let's discuss this today.
Suppose there are n persons arranged in circle and you have given them numbers i.e.
1,2,3,....n in a circle and starting from number 1 you need to eliminate every second number and you have to tell me the last person number remaining.
The fact that makes me wonder is that it has direct application to binary numbers.
How???
let's discuss this
1) When 2^n is divided by 2 it becomes either 1 or 2^n-1(2^m i.e. snumber of same form)
So to solve problem of size n you can solve with size n-1.
let's see this principle in action:-
a) in each step you divide every alternate number i.e.
if numbers are 1,2,3,4,5,6,7,8 = 8 numbers
after first step numbers remaining will be:-
1,3,5,7 = 4 numbers and you again start from index==1.
I mean you keep iterating this again and again.
and finally the number remaining is 1.
i.e. answer = 1.
b) If any number which is not of the form 2^n then this number can be written in the form of 2^n
i.e.
num = 2^n + m;
let's eliminate m numbers first i.e. to eliminate m numbers we have reached at index = 2*m and now starting at index 2*m+1.
but at this point total numbers in circle are 2^n and then starting index of circle is 2*m+1 and the length of circle is 2^n i.e. number with form of power of 2.
implies answer in this case is 2*m+1.

I know that this might be complicating to understand when i'lkl be reading it again but just want to make note of what i have done for the day.

Thanks!

No comments:

Post a Comment