# Doors in school

In the morning all the doors in the school are closed. The school is quite big: there are N doors. Then pupils start coming. It might be hard to believe, but all of them want to study! Also, there are exactly N children studying in this school, and they come one by one.

When these strange children pass by some doors they change their status (i.e. Open -> Closed, Closed -> Open). Each student has their number, and each i-th student alters the status of every i-th door. For example: when the first child comes to the schools, he changes every first door (he opens all of them). The second one changes the status of every second door (he closes some doors: the 2nd, the 4th and so on). Finally, when the last one – the n-th – comes to the school, he changes the status of each n-th door (there's only one such door, though).

We need to count how many doors are left opened after all the students have come.

### Example

doors(5)

0 | 0 | 0 | 0 | 0 |
---|---|---|---|---|

1 | 1 | 1 | 1 | 1 |

1 | 0 | 1 | 0 | 1 |

1 | 0 | 0 | 0 | 1 |

1 | 0 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 0 |

Answer is 2

### Solution

Using JS

```
let n = 5;
let doors = new Array(n).fill(0);
for (let x = 1; x <= n; x++) {
for (let y = 1; y < n; y=y+x) {
if (x !== 1 && y === 1) continue;
doors[y] = doors[y] === 0 ? 1 : 0;
}
}
let res = doors.reduce((a, b) => a+b,0);
console.log(res);
```

Using GPT3.5

```
let n = 5;
let doors = new Array(n).fill(0);
for (let x = 1; x <= n; x++) {
for (let y = x - 1; y < n; y += x) {
doors[y] ^= 1;
}
}
let res = doors.reduce((a, b) => a + b, 0);
console.log(res);
```