Joseph ring problem algorithm