Problem Statement. There are many interesting variations on the Tower of Hanoi problem. This version consists of $N$ pegs and one ball containing each number from $1, 2, 3, \dots$ Whenever the sum of the numbers on two balls is not a perfect square (i.e., $c^2$ for some integer $c$), they will repel each other with such force that they can never touch each other.
The player must place balls on the pegs one by one, in order of increasing ball number (i.e., first ball $1$, then ball $2$, ball $3$ $\dots$). The game ends where there is no non-repelling move. The goal is to place as many balls on the pegs as possible. The figure above gives a best possible result for $N = 4$ pegs. One may assume $N \le 50$ in all test cases.
Source: https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1217.
I originally encountered this problem in the book "Programming Challenges" from Skiena and Revilla where it is part of Chapter 9: "Graph Traversal" given a short hint:
Can the constraints be usefully modeled using a DAG?
Question 1. How to use this hint?
Last but not least, I observed the solution seems to be always
$\displaystyle \left\lfloor \frac{(N + 1)^2}{2} \right\rfloor - 1$
which is the reason I ask this question.
Question 2. How to prove this formula or at least some upper bound on the number of balls depending on $N$?
I appreciate any kind of help in getting more out of this problem. Thanks!