1

I encountered two types of Miller Rabin primality test methods suddenly. One which uses randoms and another does not use randoms.

Is there a hidden random generation inside the second one or what? Thank you.

Community
  • 1
  • 1
  • Thank you very much. Simple comparison of both yields that the probabilistic algorithm is much faster and I can understand why the deterministic algorithm isn't much popular. – Isura Manchanayake Dec 08 '16 at 21:04
  • Deterministic M-R testing is less popular mostly because people don't know about it. Especially using more optimal sets, they gives very efficient deterministic answers for all 64-bit inputs, with fewer tests needed than needed for reliable results using probabilistic tests for this size. So it is in fact faster in practice (unless you don't care about correct results). – DanaJ Dec 26 '16 at 04:41
  • yes.. I did a kind of research on comparing deterministic MR and the square root bounding method here https://isuramanchanayake.wordpress.com/2016/12/08/algorithms/#graphical-comparison . There I've found that det-MR is far better for large inputs as you say and not much efficient for values under 10^12 – Isura Manchanayake Dec 26 '16 at 04:51
  • It will depend strongly on your implementations. The crossover vs. a mod-30 wheel trial division is 500,000 for my code, but it is in C using a single-base hashed solution for 32-bit inputs and asm mulmod so much faster. – DanaJ Dec 26 '16 at 04:58
  • what is a single-base hashed solution? – Isura Manchanayake Dec 26 '16 at 05:42
  • See http://miller-rabin.appspot.com/. Your trial division routine can be faster as well (uint32_t type if possible, split out more small divisors, unroll loop). In your mulmod routine it's possible to entirely remove the expensive mod operations. If your compiler has uint128_t support it's often faster to use that. Even faster is asm on x86_64 or PPC, but obviously not portable. – DanaJ Dec 26 '16 at 07:23
  • Thank you very much.. btw can I see your code? – Isura Manchanayake Dec 26 '16 at 13:48

1 Answers1

2

The second one is a deterministic variant of the Miller-Rabin primality test. Instead of using "witness" numbers generated from random numbers, a list of primes that are known to be sufficient is used instead:

When the number n to be tested is small, trying all a < 2(ln n)2 is not necessary, as much smaller sets of potential witnesses are known to suffice"

if n < 3,825,123,056,546,413,051, it is enough to test a = 2, 3, 5, 7, 11, 13, 17, 19, and 23.

This is the list of primes in alist in the linked source code.

Community
  • 1
  • 1
samgak
  • 23,944
  • 4
  • 60
  • 82