triangle algorithm