# Python 2, <s>67</s> 65 bytes f=lambda n,k=0,m=2,p=1:k/n*~-m or f(n,k+p%m*(`n`in`m`),m+1,p*m*m) Test it on [Ideone]. This uses a corollary of [Wilson's theorem]: ![corollary of Wilson's theorem][corollary] [Ideone]: http://ideone.com/epHD9u [Wilson's theorem]: https://en.wikipedia.org/wiki/Wilson%27s_theorem [corollary]: https://i.sstatic.net/mPJHD.png