How to resolve the algorithm 100 doors step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 100 doors step by step in the Prolog programming language

Table of Contents

Problem Statement

There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it.
The third time, visit every 3rd door   (door #3, #6, #9, ...), etc,   until you only visit the 100th door.

Answer the question:   what state are the doors in after the last pass?   Which are open, which are closed?

Alternate:
As noted in this page's   discussion page,   the only doors that remain open are those whose numbers are perfect squares. Opening only those doors is an   optimization   that may also be expressed; however, as should be obvious, this defeats the intent of comparing implementations across programming languages.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 100 doors step by step in the Prolog programming language

Source code in the prolog programming language

main :-
    forall(between(1,100,Door), ignore(display(Door))).

% show output if door is open after the 100th pass
display(Door) :-
    status(Door, 100, open),
    format("Door ~d is open~n", [Door]).

% true if Door has Status after Pass is done
status(Door, Pass, Status) :-
    Pass > 0,
    Remainder is Door mod Pass,
    toggle(Remainder, OldStatus, Status),
    OldPass is Pass - 1,
    status(Door, OldPass, OldStatus).
status(_Door, 0, closed).

toggle(Remainder, Status, Status) :-
    Remainder > 0.
toggle(0, open, closed).
toggle(0, closed, open).

doors_unoptimized(N) :-
	length(L, N),
	maplist(init, L),
	doors(N, N, L, L1),
	affiche(N, L1).

init(close).

doors(Max, 1, L, L1) :-
	!,
       inverse(1, 1, Max, L, L1).

doors(Max, N, L, L1) :-
	N1 is N - 1,
	doors(Max, N1, L, L2),
	inverse(N, 1, Max, L2, L1).


inverse(N, Max, Max, [V], [V1]) :-
	!,
	0 =:= Max mod N -> inverse(V, V1); V1 = V.

inverse(N, M, Max, [V|T], [V1|T1]) :-
	M1 is M+1,
	inverse(N, M1, Max, T, T1),
	(   0 =:= M mod N -> inverse(V, V1); V1 = V).


inverse(open, close).
inverse(close, open).

affiche(N, L) :-
	forall(between(1, N, I),
	       (   nth1(I, L, open) -> format('Door ~w is open.~n', [I]); true)).

doors(Num, Passes) :-
    forall(( everyNth(1,Passes,1,Pass)
           , forall((everyNth(Pass,Num,Pass,Door), toggle(Door)))
           ))
  , show(Num)
  .


toggle(Door) :-
    Opened = opened(Door)
  , ( clause(Opened,_) -> retract(Opened)
                        ; asserta(Opened)
    ).


show(Num) :-
    forall(( between(1,Num,Door)
           , (opened(Door) -> State = opened ; State = closed)
           , write(Door), write(' '), write(State), nl
           )).


% utils
forall(X) :- findall(_, X, _).

everyNth(From,To,Step,X) :-
    From =< To
  , ( X = From ; From1 is From + Step, everyNth(From1,To,Step,X) )
  .

main :- doors(100,100), halt.

doors_optimized(N) :-
	Max is floor(sqrt(N)),
	forall(between(1, Max, I),
	       (   J is I*I,format('Door ~w is open.~n',[J]))).

  

You may also check:How to resolve the algorithm Bitmap step by step in the C++ programming language
You may also check:How to resolve the algorithm Window creation step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Fortran programming language
You may also check:How to resolve the algorithm Variable size/Get step by step in the Scala programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the C programming language