How to resolve the algorithm 100 doors step by step in the OCaml programming language
How to resolve the algorithm 100 doors step by step in the OCaml 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 OCaml programming language
Source code in the ocaml programming language
let max_doors = 100
let show_doors =
Array.iteri (fun i x -> Printf.printf "Door %d is %s\n" (i+1)
(if x then "open" else "closed"))
let flip_doors doors =
for i = 1 to max_doors do
let rec flip idx =
if idx < max_doors then begin
doors.(idx) <- not doors.(idx);
flip (idx + i)
end
in flip (i - 1)
done;
doors
let () =
show_doors (flip_doors (Array.make max_doors false))
let optimised_flip_doors doors =
for i = 1 to int_of_float (sqrt (float_of_int max_doors)) do
doors.(i*i - 1) <- true
done;
doors
let () =
show_doors (optimised_flip_doors (Array.make max_doors false))
type door = Open | Closed (* human readable code *)
let flipdoor = function Open -> Closed | Closed -> Open
let string_of_door =
function Open -> "is open." | Closed -> "is closed."
let printdoors ls =
let f i d = Printf.printf "Door %i %s\n" (i + 1) (string_of_door d)
in List.iteri f ls
let outerlim = 100
let innerlim = 100
let rec outer cnt accu =
let rec inner i door = match i > innerlim with (* define inner loop *)
| true -> door
| false -> inner (i + 1) (if (cnt mod i) = 0 then flipdoor door else door)
in (* define and do outer loop *)
match cnt > outerlim with
| true -> List.rev accu
| false -> outer (cnt + 1) (inner 1 Closed :: accu) (* generate new entries with inner *)
let () = printdoors (outer 1 [])
You may also check:How to resolve the algorithm Program termination step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Arrays step by step in the PL/I programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Plain English programming language
You may also check:How to resolve the algorithm Abbreviations, easy step by step in the Rust programming language
You may also check:How to resolve the algorithm Box the compass step by step in the K programming language