improving: beads.java

a better O(n^2)