Prime number just below a number [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-13 17:14:33

问题


I want to calculate prime number just below a number. How can this be done efficiently.

I used Sieve of Eratosthenes but it failed as my numbers are in range 10^20

Any other algorithm??


回答1:


The chance of a random 20-digit number being prime is approximately 1/20 (source). If you want the largest prime below x, start at x-1 and run a primality test on each number, working your way down until you find a prime. The following is a related answer I gave and lists a highly reliable and extremely fast pseudoprime test that should suffice:

how to test a prime number 1000 digits long?




回答2:


Use a segment of a sieve for sufficiently many numbers just below n so that you can be reasonable sure it contains a prime (say a couple of thousand numbers). Then sieve with small primes to remove most of the non-primes. Use a full primality test on the remaining numbers (either probabilistic, or for 20 digits trial division up to the root might still be reasonable).

How many small primes you use for sieving depends on the relative cost of sieving versus running the full primality test.



来源:https://stackoverflow.com/questions/16387319/prime-number-just-below-a-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!