created recusion loop to solve n-x problem