ONE HUNDRED AND NINETY FOUR

From Angus Croll’s If Hemingway Wrote Javascript: Explained.

Borges’ solution is a variation on the Sieve of Eratosthenes algorithm by which the multiples of each known prime are marked as composite (non-prime). In this case, Borges has long legged monsters take the place of divisors. Each monster straddles one more stair than the monster that went before: 2, 3, 4, 5…up to the square root of the number of the highest stair. (for non-obvious reasons, Borges allows composite-gaited monsters to climb the stairs too). The untrodden stairs are the prime numbers.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

// They speak (I know) of finials, newels and balustrades

// of hidden spandrels and eternally clambering, broad-gaited beasts…

 

var monstersAscendingAStaircase = function(numberOfSteps) {

  var stairs = []; stepsUntrodden = [];

  var largestGait = Math.sqrt(numberOfSteps);

 

  // A succession of creatures mount the stairs;

  // each creature’s stride exceeds that of its predecessor

  for (var i = 2; i <= largestGait; i++) {

    if (!stairs[i]) {

      for (var j = i * i; j <= numberOfSteps; j += i) {

        stairs[j] = “stomp”;

      }

    }

  }

 

  // Long-limbed monsters won’t tread on prime numbered stairs.

  for (var i = 2; i <= numberOfSteps; i++) {

    if(!stairs[i]) {

      stepsUntrodden.push(i);

    }

  }

 

  // Here, then, is our answer.

  return stepsUntrodden;

};