NUMFACT - Time limit exceed error

It gives a TLE when i use the function ‘prime’ to find the prime factorisation of the number.Can someone please tell me where i went wrong? Thanks in advance.
Here is the link to my code :

Hi, Kindly post your query on the editorial page of the problem here: