Function Resource

RandomSafePrime

Compute one or more safe primes in a requested range

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

ResourceFunction["RandomSafePrime"] uses RandomPrime to generate candidate safe primes. Generating the requested number of safe primes may take a significant amount of time. Ranges which contain no safe primes will eventually time out.
RandomSafePrime will time out after 60 seconds. This time-out can be modfied with the TimeConstraint option.

Examples

Basic Examples

Generate a random safe prime:

In[1]:=
p = ResourceFunction[ "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/\
Function/RandomSafePrime"][2^64]
Out[1]=

Check that it is indeed a safe prime:

In[2]:=
ResourceFunction[
ResourceObject[
Association[ "Name" -> "SafePrimeQ", "ShortName" -> "SafePrimeQ", "UUID" -> "771122ee-cbcc-4893-a2f9-dc488d210486", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Determine whether a number is a safe prime", "RepositoryLocation" -> URL[ "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$\
9e23b991252c4411863be568f6325ac8`SafePrimeQ", "FunctionLocation" -> CloudObject[ "https://www.wolframcloud.com/obj/8a5611ea-4462-4a67-a3ce-\
052cce5b4c60"]], ResourceSystemBase -> Automatic]][p]
Out[2]=

Generate several safe primes:

In[3]:=
list = ResourceFunction[ "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/\
Function/RandomSafePrime"][2^64, 10]
Out[3]=

Check that they are safe primes:

In[4]:=
Map[ResourceFunction[
ResourceObject[
Association[ "Name" -> "SafePrimeQ", "ShortName" -> "SafePrimeQ", "UUID" -> "771122ee-cbcc-4893-a2f9-dc488d210486", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Determine whether a number is a safe prime", "RepositoryLocation" -> URL[ "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$\
9e23b991252c4411863be568f6325ac8`SafePrimeQ", "FunctionLocation" -> CloudObject[ "https://www.wolframcloud.com/obj/8a5611ea-4462-4a67-a3ce-\
052cce5b4c60"]], ResourceSystemBase -> Automatic]], list]
Out[4]=

Scope

Generate a safe prime in a specific range:

In[5]:=
ResourceFunction[ "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RandomSafePrime"][{1000, 2000}]
Out[5]=

Generate several safe primes in a specific range:

In[6]:=
ResourceFunction[ "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RandomSafePrime"][{1000, 2000}, 10]
Out[6]=

Options

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

In[7]:=
ResourceFunction[ "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Function/\
RandomSafePrime"][{2^32, 2^40}, 10000, TimeConstraint -> 1]
Out[7]=