Some ruby algorithm