When I built the low-cost and precise rotary encoder with magnetic spheres I used 21 spheres because they fitted nicely in the other parts of the build I had around.
However, if an arbitrary number of spheres is to be used then an appropriate holder must be built.
The magnetic spheres are 5mm in diameter. Knowing the number and the radius of the external spheres it is possible to calculate the radius of a circle surrounded by adjacent circles by using the inverse formula for the Numbers of circles around a circle.
OpenSCAD makes it easy to build parametric objects:
I chose a number of spheres to match a minimum internal radius that was needed to fit the holder on a bike wheel, the nine couples of square holes are for tying the holder to the spokes.
The source code is in the OpenSCAD-Tetramag_spheres_holder git repository.
The end result is not bad and fits the wheel quite nicely: