No, p is not a polynomial-time algorithm. If the input to f is an integer n in base 10, the the length of the input is about log10(n). Put another way, if the input has length ℓ then the input n is roughly 10, which is not bounded above by any polynomial in ℓ.