# 100 doors

### unoptimized

```perl
my @doors = False xx 101;
 
(.=not for @doors[0, $_ ... 100]) for 1..100;
 
say "Door $_ is ", <closed open>[ @doors[$_] ] for 1..100;
```

### optimized

```perl
say "Door $_ is open" for map {$^n ** 2}, 1..10;
```

### probably the most compact idiom

```perl
say 'Door $_ is open' for (1..10)»²;
```

### Here's a version using the cross meta-operator instead of a map:

```perl
 say "Door $_ is open" for 1..10 X** 2;
```

This one prints both opened and closed doors:

```perl
say "Door $_ is ", <closed open>[.sqrt == .sqrt.floor] for 1..100;
```

### verbose version, but uses ordinary components

```perl
sub  output( @arr, $max ) {
    my $output = 1;
    for 1..^$max -> $index {
	if @arr[$index] {
	    printf "%4d", $index;
	    say '' if $output++ %%  10;
	}
    }
    say '';
}

sub MAIN ( Int :$doors = 100 ) {
    my $doorcount = $doors + 1;
    my @door[$doorcount] = 0 xx ($doorcount);
    
    INDEX:
    for 1...^$doorcount -> $index {
        # flip door $index & its multiples, up to last door.
        #
	for ($index, * + $index ... *)[^$doors] -> $multiple {
	    next INDEX if $multiple > $doors;
	    @door[$multiple] =  @door[$multiple] ?? 0 !! 1;
	}
    }
    output @door, $doors+1;
}
```

#### Output:

```
$ ./100_doors.pl6 -doors=100
   1   4   9  16  25  36  49  64  81
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/perl6-rosettacode/programming_tasks/1/100_doors.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
