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