Given an integer \$N>3\$, you have to find the minimum number of bits that need to be inverted in \$N\$ to turn it into a square number. You are only allowed to invert bits below the most significant one.
Examples
- \$N=4\$ already is a square number (\$2^2\$), so the expected output is \$0\$.
- \$N=24\$ can be turned into a square number by inverting 1 bit: \$11000 \rightarrow 1100\color{red}1\$ (\$25=5^2\$), so the expected output is \$1\$.
- \$N=22\$ cannot be turned into a square number by inverting a single bit (the possible results being \$23\$, \$20\$, \$18\$ and \$30\$) but it can be done by inverting 2 bits: \$10110 \rightarrow 10\color{red}0\color{red}00\$ (\$16=4^2\$), so the expected output is \$2\$.
Rules
- It is fine if your code is too slow or throws an error for the bigger test-cases, but it should at least support \$3 < N < 10000\$ in less than 1 minute.
- This is code-golf!
Test cases
Input | Output
----------+--------
4 | 0
22 | 2
24 | 1
30 | 3
94 | 4
831 | 5
832 | 1
1055 | 4
6495 | 6
9999 | 4
40063 | 6
247614 | 7 (smallest N for which the answer is 7)
1049310 | 7 (clear them all!)
7361278 | 8 (smallest N for which the answer is 8)
100048606 | 8 (a bigger "8")
Or in copy/paste friendly format:
[4,22,24,30,94,831,832,1055,6495,9999,40063,247614,1049310,7361278,100048606]
100048606
on TIO, is that a problem? \$\endgroup\$