faster calc of p as recommended by @ntamas