How to optimise my code for kth root