b = 0;
function PrisonerRiddle(a, b){
//a = status of the lamp
//b = number of days
b = b + 1;
if b < 100 then PrisonerRiddle(a, b);
else
//do stuff
//the solution would be something like:
if a == 0 then print("I am the last prisoner!");
end if
}
Come to think of it, I think I can do it creating threads for the prisoners, and all of them accessing the function. Which brings me to another riddle proposed by a "operating systems" profesor back in college, which I've solved by programming threads in Java. It's an easy one, and it goes like this:
Imagine that there are five filosofers sitting on a round table, each with a plate with spaghetti on it. These filosofers eat the spaghetti using 2 forks at the same time. Now, between each plate there is only one fork, so that a filosofer can see his plate, a fork on the left side, and a fork on the right side. OK? Now, every filosofer will start eating his spaghetti at a random time, eat it for a random time, and stop eating it at a random time, the proceed to eat again, ad infinitum. When a filosofer decides to eat the spaghetti, he'll reach for BOTH forks and proceed to eat. The problem is, if a filosofer grabs a fork, let's say, on his left side, the filosofer to his left can't grab his RIGHT fork, get it? Now, if a filosofer wants to eat, and he can't get both of his right and left forks, he'll just stay there, waiting for the fork. Unfortunally, this can lead to a lock up if every filosofer grabs one fork. Now, you gotta decide a special rule so that this scenario never happens, and the filosofers never lock up.