# How to plot the number of Mersenne Primes lower than a given input?

I would like to plot the number of Mersenne primes (primes of the form $2^n-1$) lower than a given input. I am totally noob to mathematica and I do not know where to start from. I have tried to implement "Range[ ]" and "Length@" functions, but I do not really know how to properly use then. Thank you

Edit: I am trying this code:

MersennePrimes[n_] := With[{primes = Prime[Range[PrimePi[n]]]}, Length@Pick[primes, MersennePrimeExponentQ[n]]];


But i get this error:

Length::argx: Length called with 0 arguments; 1 argument is expected.


Any help?

• Well, the documentation is generally a good place to start . Both reference.wolfram.com/language/ref/ListPlot.html and reference.wolfram.com/language/ref/MersennePrimeExponent.html may be useful for what you're trying to do. – user6014 Dec 5 '16 at 21:35
• While this is very easy to accomplish in Mathematica, before anyone takes the time to construct something for you, you should post some of what you've tried so far. Knowing which part of the process you're struggling with will help us better address the issues you've ran into. – user6014 Dec 5 '16 at 21:36
• @user6014 Thank you! Main post edited – user3141592 Dec 5 '16 at 21:48
• @user3141592 I understand that you don't know how to use those functions, but if I were you I would try to read the manual and see if you can figure those out. You might also be interested in PrimeQ, or in MersennePrimeExponentQ from the documentation. – MarcoB Dec 5 '16 at 21:56
• Here's a hint for a simple implementation that counts the number of Mersenne primes less than a given number. You can use Count where the first argument to Count is Range[ ...something... ] and the second argument uses MersennePrimeExponentQ. – Chip Hurst Dec 5 '16 at 22:02

## Method

There are $44$ Mersenne primes implemented in Mathematica. Their exponents are instantly returned with (the few more tentative ones can be added to this list manually)

exp = MersennePrimeExponent @ Range @ 44


{2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787, 1398269, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583, 25964951, 30402457, 32582657}

Mersenne primes are of the form $2^n-1$; one could compute them all with 2^exp-1, but the 44th Mersenne prime has nearly 10 million digits, so I don't recommend it (MMA can handle their display, though). Nevertheless, because they are of the form $2^n-1$, which is a monotonic function, it is sufficient to translate x - i.e., the input smaller of which you want to find the number of primes - to Log2[x + 1].

## Function

One can make a function:

f[x_] := Count[exp, u_ /; u < Log2[x + 1]]


Then:

f


0

f


0

f


2

f


3

f


5

f


5

f


6

and for example

f


12

## Plotting

plot1 = LogLinearPlot[f[x], {x, 1, 10^20},  Frame -> True, PlotRange -> Full];
plot2 = LogLinearPlot[f[x], {x, 1, 10^300}, Frame -> True, PlotRange -> Full];
GraphicsRow[{plot1, plot2}, ImageSize -> 800] The horizontal axis displays the exponent for the prime in Log10 basis. MMA is not able to go higher than the range showed in plot2. Note that, as mentioned,

Log10[2^Last[N @ exp]]


9.80835709543099*10^6

meaning that the 44th Mersenne prime has almost 10 million digits, being ridiculously big.