simple algorithm learning exercise