implemented triangle algorihtm