On Sat, Jan 21, 2012 at 4:27 PM, Zhi-Qiang Lei <zhiqiang.lei at gmail.com> wrote: > Could anyone give me some hints to enhance it? Thanks. You could use a faster primality test algorithm such as Miller-Rabin [1]. [1] http://en.wikipedia.org/wiki/Miller–Rabin_primality_test