Various Algorithms in PHP