c++,algorithm,random-sample,primality-test

You might want to take a look at the Miller-Rabin primality test. In this test you use a series of "witness" values and perform some calculations. Each witness calculation gives a result of "composite" or "possibly prime". If you use k witnesses and they all give "possibly prime" results, the...

In your try_composite function, the for loop should be for i in range(1,s). Do not test the case where i is zero. EDIT: Also, you are missing a test in your try_composite function. Here is my version of the pseudocode: def isPrime(n, k=5): def isComposite(s, d): x = pow(randrange(2,n-1), d,...

language-agnostic,primality-test

Yes, all primes are of the form 6k +/- 1, but that doesn't mean that each number that is of the form 6k +/- 1 is prime. Consider 25, which is 6 * 4 + 1. Clearly, 25 is not prime.

For the Sieve of Erastothenes you are recomputing the sieve every time. The sieve should be cached so that you only generate it once. It works well when you build the sieve once and then perform many primality checks; it is very inefficient if you only check a single number....