Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Light
08-08-2009, 12:07 AM (This post was last modified: 08-08-2009 12:09 AM by drdebcol.)
Post: #1
Light
There is man named Mabu who switches on-off the lights along a corridor at one university. Every bulb has its own toggle switch that changes the state of the light. If the light is off, pressing the switch turns it on. Pressing it again will turn it off. Initially each bulb is off. He does a peculiar thing. If there are n bulbs in a corridor, he walks along the corridor back and forth n times. On the ith walk, he toggles only the switches whose position
is divisible by i. He does not press any switch when coming back to his initial position. The ith walk is defined as going down the corridor (doing his peculiar thing) and coming back again. Determine the final state of the last bulb. Is it on or off?
Input
The input will be an integer indicating the n-th bulb in a corridor.
Output
Output “yes” or “no” to indicate if the light is on, or no !
So here is solution in Pascal :
Code:
program light;
uses wincrt;
var
n,i:integer;
a:array[1..10000] of boolean;
procedure fill_with_true;
var
  i:integer;
begin
  for i:=1 to 10000 do
  a[i]:=true;
end;
function check(k:integer):boolean;
var
  i,j:integer;
begin
  for i:=1 to k do
    for j:=1 to k do
      begin
        if j mod i = 0 then
          if a[i]=true then
            a[i]:=false
          else
            a[i]:=true;
      end;
    if a[k]=true then
      check:=true
    else
      check:=false;
end;
begin
writeln('enter n ');
readln(n);
fill_with_true;
if check(n) then
writeln('yes')
else
writeln('no');
end.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: