(Used in the 2006 Baldwin-Wallace College High School Programming Competition)
See the problem here (Problem 5)