## Peter moves in a hallway with $N + 1$ doors consecutively numbered from $0$ through $N$. All doors are initially closed. Peter starts in front of door $0$, and repeatedly performs the following steps:

First, he walks a positive square number of doors away from his position.

Then he walks another, larger square number of doors away from his new position.

He toggles the door he faces (opens it if closed, closes it if open).

And finally returns to door $0$.

We call an action any sequence of those steps. Peter never performs the exact same action twice, and makes sure to perform all possible actions that don’t bring him past the last door.

Let $F(N)$ be the number of doors that are open after Peter has performed all possible actions. You are given that $F(5) = 1$, $F(100) = 27$, $F(1000) = 233$ and $F(10^6) = 112168$.

Find $F(10^{12})$.

### To solve this problem, we need to simulate Peter’s movements and keep track of the state of each door.

We can start by writing a function to determine if a number is a perfect square:

“`python

import math

def is_perfect_square(n):

root = int(math.sqrt(n))

return root * root == n

“`

Next, we can define a function to simulate Peter’s movements and count the number of open doors:

“`python

def count_open_doors(N):

doors = [False] * (N + 1) # Initialize all doors as closed

for i in range(N + 1):

if is_perfect_square(i): # Peter starts at door 0 before each action

doors[i] = not doors[i] # Toggle the door

for j in range(1, int(math.sqrt(i)) + 1): # Walk a positive square number of doors away

next_door = i + j * j

if next_door <= N:
doors[next_door] = not doors[next_door] # Toggle the door
return sum(doors) # Count the number of open doors
```
Finally, we can call the `count_open_doors` function to find the answer for $F(10^{12})$:
```python
N = int(1e12)
result = count_open_doors(N)
print(result)
```
This code will calculate the number of open doors for $F(10^{12})$. However, keep in mind that the execution time will be quite long due to the large value of $N$.

##### More Answers:

Divisor Sums$\pi$ Sequences

Roman Numerals II