TEIRESIAS algorithm