I tried to solve it without regexp at first, failed brutally. Cut the line count by 80% using a regexp instead. It was also funny to get all square roots.
Brute force, since I was to newly awake to figure out the smart solution.