code of the book Algorithm version 4