# Of what use is my code for finding prime numbers of a certain size? [closed]

I've developed a bit of Mathematica code that can find primes within a range of numbers. For example, if I wanted all the primes between one million and two million, it could do that. Of what use is this code I invented ?

The only thing I could think of was that if one wanted to find two primes of a certain size for cryptography keys.

And the code could be used to test a specific number for primality, quickly.

And with the addition of code, it could be made to search for primes of a certain format, like safe primes.

I just invented this last week

Copy the following into Mathematica, select all, right-click on Convert To, choose StandardForm.

N[Sum[2/10^((n*(Floor[c/n] + 1) - c)*s), {n, 1, Floor[Sqrt[ c]]}] + Sum[ 2/(10^((n*(Floor[c/n] + 1) - c)*s)* (10^(ns) - 1)), {n, 1, Floor[Sqrt[ c]]}] + Sum[2/(10^((o^2 - o - c) s)*(10^(o*s) - 1)), {o, Floor[Sqrt[c]] + 1, max}], ((max + 1)^2 - c)*s]; AbsoluteTiming[Flatten[ Position[Partition[ RealDigits[%][], s, s, -1], {(0) .., 2}]]] + c -end c=crossover s=spacing

The crossover is the number which you want to surpass. The spacing is difficult to determine, so I'll say that in general, if you use a number which is equal to the number of digits of the max^2, you should be safe. So for all primes up to $100^2 = 10000$, that is five digits, so use a 5 for the spacing. Follow this rule and you should be safe.

For those who are wondering, the code is based on a bigger structure of numbers formed by the Sieve of Eratosthenes.

## closed as off-topic by yyyyyyy, e-sushiFeb 24 '17 at 0:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Programming questions are off-topic even if you are writing or debugging cryptographic code. Unless your question is specifically about how the cryptographic algorithm or protocol works, you should look into asking on Stack Overflow instead." – yyyyyyy, e-sushi
If this question can be reworded to fit the rules in the help center, please edit the question.

• In Mathematica, RandomPrime[{$i_\text{min}$,$i_\text{max}$}] gives a pseudorandom prime number in the range {$i_\text{min}$,$i_\text{max}$}. – fgrieu Mar 13 '15 at 19:35
• PrimeQ is also of interest. – Thomas Mar 14 '15 at 4:02
• I am not a Mathematica user, so I might be jumping to conclusions. However I strongly suspect it is possible to restructure your code to make it more readable. As for performance, how quickly would your code be able to find all the safe primes in a range such as $2^{8192}\pm 2^{28}$? – kasperd Mar 14 '15 at 14:33
• @kasperd , I might be able to do that. That's a larger range of numbers than I've ever tried before though. Might take a day. – user24719 Mar 19 '15 at 3:30

## 1 Answer

I'm sorry to say that your code is likely to have essentially zero use.

Primes used for cryptography (e.g., RSA), are on the order of 2,048 and 4,096 bits of length, or respectively roughly 616 and 1,233 digits long. Algorithms already exist to rapidly find (random) primes of this size, and unless you've broken new ground in number theory, your approach is slower.

Additionally, the general trend lately is moving away from systems like RSA towards elliptic-curve based cryptosystems which have no need for the generation of primes.

• RSA moduli today are 2048 bits and up; the two prime factors are half the modulus size. In addition to time, if this code like a standard sieve would need e.g. 2^512 bits of memory, that's not only more than you can fit on any single computer it's hugely more than you can fit on this planet. – dave_thompson_085 Mar 13 '15 at 23:22
• @dave_thompson_085 Or even in this universe... – Thomas Mar 14 '15 at 2:57
• @Stephen Touset , Actually it would be fairly easy for me to find all the primes between 616 digits and 1233 digits. It wouldn't be rapidly but it would find them all. – user24719 Mar 19 '15 at 3:35
• @user24719 "it wouldn't be rapid" is an understatement, there physically wouldn't be enough energy in the entire universe to carry out the computation. – Thomas Mar 19 '15 at 23:17
• Nor enough storage capacity to record them all. – Stephen Touset Mar 20 '15 at 17:16