Translates Algorithm3's code to PHP/