Counting arrays with Euclidean distance at most 2 from a given binary array
Don't search for a formula&nbsp;&ndash; you'll probably never find something so specific. Instead, try to break up the task into smaller units.

Since your arrays are binary,
$$(A_i-B_i)^2 = \begin{cases}0 &amp;\text{if }A_i=B_i\\ 1&amp;\text{if }A_i\neq B_i\,.\end{cases}$$

So $\sqrt{\sum(A_i-B_i)^2}\leq 2$ if, and only if there are at most four values of&nbsp;$i$ such that $A_i\neq B_i$.  So you just need to compute the number of ways that $B$&nbsp;could have zero, one, two, three or four different entries from&nbsp;$A$ (or, more simply, the number of ways that it could have five or six different entries, and subtract that from the total possible values of&nbsp;$B$). This calculation is high-school combinatorics. Note that, because the arrays are binary, there's only one value of&nbsp;$B_i$ that's the same as&nbsp;$A_i$ and only one that's different.