Function Repository Resource:

RandomSafePrime

Source Notebook

Compute one or more safe primes in a requested range

Contributed by: Arnoud Buzing

ResourceFunction["RandomSafePrime"][max]

gives a safe prime equal or less than max.

ResourceFunction["RandomSafePrime"][{min,max}]

gives a safe prime in the range min to max.

ResourceFunction["RandomSafePrime"][range,n]

gives n safe primes in the specified range.

Details and Options

A safe prime is a prime number of the form 2p+1, where p is also a prime.
ResourceFunction["RandomSafePrime"] uses RandomPrime to generate candidate safe primes. Generating the requested number of safe primes may take a significant amount of time. Ranges that contain no safe primes will eventually time out.
ResourceFunction["RandomSafePrime"] will time out after 60 seconds. This time-out can be modfied with the TimeConstraint option.

Examples

Basic Examples (2) 

Generate a random safe prime:

In[1]:=
p = ResourceFunction["RandomSafePrime"][2^64]
Out[1]=

Check that it is indeed a safe prime using the resource function SafePrimeQ:

In[2]:=
ResourceFunction["SafePrimeQ"][p]
Out[2]=

Generate several safe primes:

In[3]:=
list = ResourceFunction["RandomSafePrime"][2^64, 10]
Out[3]=

Check that they are safe primes via the resource function SafePrimeQ:

In[4]:=
Map[ResourceFunction["SafePrimeQ"], list]
Out[4]=

Scope (2) 

Generate a safe prime in a specific range:

In[5]:=
ResourceFunction["RandomSafePrime"][{1000, 2000}]
Out[5]=

Generate several safe primes in a specific range:

In[6]:=
ResourceFunction["RandomSafePrime"][{1000, 2000}, 10]
Out[6]=

Options (1) 

Attempting to generate a large number of safe primes in a small amount of time will time out:

In[7]:=
ResourceFunction["RandomSafePrime"][{2^32, 2^40}, 10000, TimeConstraint -> 1]
Out[7]=

Publisher

Arnoud Buzing

Version History

  • 1.0.0 – 08 May 2020

Related Resources

License Information