code for X13 algorithm