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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 100 doors step by step in the Racket 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 Racket programming language

Source code in the racket programming language

#lang racket

;; Applies fun to every step-th element of seq, leaving the others unchanged.
(define (map-step fun step seq)
  (for/list ([elt seq] [i (in-naturals)])
    ((if (zero? (modulo i step)) fun values) elt)))

(define (toggle-nth n seq)
  (map-step not n seq))

(define (solve seq)
  (for/fold ([result seq]) ([_ seq] [pass (in-naturals 1)])
    (toggle-nth pass result)))

(for ([door (solve (make-vector 101 #f))] [index (in-naturals)]
      #:when (and door (> index 0)))
  (printf "~a is open~%" index))

#lang racket
(for ([x (in-range 1 101)] #:when (exact-integer? (sqrt x)))
  (printf "~a is open\n" x))

#lang slideshow
(define-syntax-rule (vector-neg! vec pos)
  (vector-set! vec pos (not (vector-ref vec pos))))

(define (make-doors)
  (define doors (make-vector 100 #f))
  (for* ([i 100] [j (in-range i 100 (add1 i))]) (vector-neg! doors j))
  doors)

(displayln (list->string (for/list ([d (make-doors)]) (if d #\o #\-))))

(define closed-door (inset (filled-rectangle 4 20) 2))
(define open-door (inset (rectangle 4 20) 2))

(for/fold ([doors (rectangle 0 0)]) ([open? (make-doors)])
  (hc-append doors (if open? open-door closed-door)))

  

You may also check:How to resolve the algorithm Caesar cipher step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Runtime evaluation step by step in the Java programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Prolog programming language
You may also check:How to resolve the algorithm HTTPS/Authenticated step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Brainf*** programming language