first version of itenary algorithm