lundi 17 août 2020

Random primes and Rabin Karp substring search

I am reading the Rabin-Karb algorithm from Sedgewick. The book says:

We use a random prime Q taking as large a value as possible while avoiding overflow

At first reading I didn't notice the significance of random and when I saw that in the code a long is used my first thoughts were:
a) Use Eratosthene's sieve to find a big prime that fits a long
or
b) look up from a list of primes any prime large enough that is greater than int and use it as a constant.

But then the rest of the explanation says:

We will use a long value greater than 10^20 making the probability that a collision happens less than 10^-20

This part got me confused since a long can not fit 10^20 let alone a value greater than that. Then when I checked the calculation for the prime the book defers to an exercise that has just the following hint:

A random n-digit number is prime with probability proportional to 1/n

What does that mean?

So basically what I don't get is:
a) what is the meaning of using a random prime? Why can't we just pre-calculate it and use it as a constant?
b) why is the 10^20 mentioned since it is out of range for long?
c) How is that hint helpful? What does it mean exactly?




Aucun commentaire:

Enregistrer un commentaire