Algorithm for Simply 3 (R Code)