💾 Archived View for kenogo.org › literate_programming › 100_doors.c captured on 2024-03-21 at 15:03:55.

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

#include <stdio.h>
#define NUM_DOORS 100
#define NUM_PASSES 100
int main()
{
  int is_open[NUM_DOORS] = { 0 };
  for (int pass = 0; pass < NUM_PASSES; pass++)
    {
      for (int door = pass; door < NUM_DOORS; door += (pass + 1))
        {
  	is_open[door] = !is_open[door];
        }
    }
  printf("The following doors are open:");
  for (int door = 0; door < NUM_DOORS; door++)
    {
      if (is_open[door])
        {
  	printf(" %d", (door + 1));
        }
    }
  printf("\n");
  return 0;
}