Code for Algorithms 4 edition.