Triangle Algorithm Implementation