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!

Wednesday, 4 May 2016

Log interface

Hi Everyone,

I am writing this to explain what is Log class that might help me later for other tutorials.

Purpose:-
Well Log class wil be used to log the information and abstract out the details to print information.
I know we don't require this class as these kind of things are already available in programming languages but this one is for me so that I can abstract out the details to log everything and can clear them with simple interface.

Responsibility:-
Responsibility of  Log will be to store the message and log the information when required.

OOD:-
State:-
Well I'll use one buffer to hole the message stored yet in log.

Behaviour:-
Class must provide its client the services to add logs, delete everything and when asked return everything available in buffer.

I love abstraction but I also feel that I must know what's happening behind the stage.
So once implemented I am satisfied now that in upcoming blog writes I can use this Log without worrying about how it works.

Implementation:-

class Log {
//buffer that will hold the log messages
private StringBuffer message;

//to initialise buffer
public Log() {
      message = new StringBuffer("");
}

//appends the information
public void appendInfo(String info) {
      message.append(info);
}


//clears the log
public void clearLogs() {
       message =  new StringBuffer("");
}

//returns the information available in log
public String getinfo() {
    return message.toString();
}
}

In short, Today I wrote the class which helps to add the log information and give me logs available when demanded.