Implement berkovitz algorithm