Optimal Mersenne Primes Search Using Modular Groups

I just released this paper (extracted from a larger journal article slated for Dec 2026) which uses Crystal code to show how to sequentially generate Mersenne Primes without computing 2^p - 1.

The code is the single core version. I haven’t had a chance yet to do the multi-core parallel version.

As these numbers become humongously large, it would require special software to generate and check 100s of digits values.

It would be nice if there could be some Crystal hooks into such existing software, to be able to write and run complete MPGS searches with it. So if anyone knows how to do that, or feels inspired to try, let me know.

https://www.academia.edu/178745494/Optimal_Mersenne_Primes_Search_Using_Modular_Groups

I realize you probably don’t want this, but it’s still better than no one replying at all.
So, I’m acting as a meat proxy :meat_on_bone: here and posting the results of ChatGPT’s review so that visitors to this page can read them.

Claims of the paper

  • The paper proposes generating Mersenne prime candidates using the exponent m of a known Mersenne prime, updating m whenever a new one is found.
  • It claims that avoiding prime exponent enumeration and reducing candidate density makes the method faster than GIMPS.

Algebraic relationship

  • The formula is G_m(k) = 2^m * (3k+1) - 1. For odd m and odd q > m, G_m(k) = 2^q-1 requires k = (2^(q-m)-1)/3.
  • OEIS A002450 gives a_j = (4^j-1)/3 (j >= 1), producing 1,5,21,85,....
  • Since G_m(a_j) = 2^(m+2j)-1, this is equivalent to enumerating odd exponents m+2,m+4,.... The identity also holds for composite Mersenne numbers and does not establish primality.

Prior work

  • For m = 3, the values of k producing Mersenne primes through 24k+7 appear in OEIS A124477 (Artur Jasinski, 2006), matching the paper’s table.
  • The general formula and the procedure for updating m also appear in the author’s earlier paper (revised 2019, published 2020).

Computational complexity

  • The paper’s k += 2 implementation requires (2^(q-m)+2)/6 iterations to reach exponent q, growing exponentially with q-m.
  • From m = 31 to q = 61, this requires 178,956,971 iterations, compared with only 7 prime exponents in the same range.
  • The author’s 2024 implementation uses k_next = 4*k+1, reducing this to 15 iterations, or 7 candidates requiring primality testing after filtering prime exponents.
  • Direct enumeration of prime exponents produces the same 7 candidates. GIMPS additionally uses factorization methods, PRP tests, and Lucas–Lehmer tests.
  • Therefore, reducing candidate density among all integers does not, by itself, demonstrate a computational advantage over existing methods.

I don’t understand any of the mathematical content. I simply invested in tokens and managed the direction of the answers through chat :melting_face:

That’s really interesting AI “commentary” . It would be really interesting if it could design an optimized distribute GIMPS like network (in whatever language(s)).

The main benefits of this method are:

  1. You don’t have to generate and verify a list of sequential candidate primes to use as p values, which is a resource intensive undertaking of itself, thus freeing up the work|time used to first find them, e.g. the memory needed to hold the primes list of ever increasing candidate primes values.

  2. Only one generator form is needed, which uses a single starting known prime p value. The k values can be generated|tested in parallel, making it amenable to massive multi core systems like GPUs, HPCs, etc

  3. The process not only finds Mersenne Primes, but other large residue primes between the consecutive Mp values. Thus the first prime found with this process greater than the current largest Mp will be the next largest known prime, but likely not an Mp.

  4. With additional research, it’s highly likely a way to reduce the number ranges for the k values can be found to narrow the search scope, thus increasing the search speed.

The GIMPS software uses specialized apps that can compute and do primality test on these large integer numbers. I’m not aware of anything in the Crystal ecosystem equivalent to them, though over time, maybe there would be interest to provide some of them.

One of the beautiful advantages of Crystal language wise is, it’s soooo easy to at least prototype working software (as shown in the paper) to demonstrate it’s validity for small values. It then merely becomes scaling it up to perform with very large numbers.

The math is far beyond my understanding but I’m stoked that you’re using Crystal to do this work!

Ok, major upgrade.

I figured out how to do away with using|iterating over k values. I explain the process in the updated paper, which has this updated, orders-of-magnitude faster code.

require "big"
require "primes-utils"  # provides ‘prime?'

def mpg(p, m)  mp = (1.to_big_i << (p + m)) - 1 end

p  = ARGV.size > 0 ? (ARGV[0].to_u64 underscore: true) : 3u64
m  = 2u64
tm = Time.instant
puts "Starting search for next Mp with known p = #{p}"

loop do
  mp = mpg(p, m)
  if mp.prime?
    p = (Math.log(mp + 1)/Math.log(2)).to_u64
    pp [p, m, mp]
    puts "Elapsed time = #{Time.instant - tm }"
    puts "\nStarting search for next Mp with known p = #{p}\n"
    m  = 2u64
    tm = Time.instant
  else
    m += 2
  end
end

It now produces scarily :face_with_peeking_eye: fast times.

➜ ./mpgs                                       
Starting search for next Mp with known p = 3
[5, 2, 31]
Elapsed time = 00:00:00.000057178

Starting search for next Mp with known p = 5
[7, 2, 127]
Elapsed time = 00:00:00.000024556

Starting search for next Mp with known p = 7
[13, 6, 8191]
Elapsed time = 00:00:00.000020819

Starting search for next Mp with known p = 13
[17, 4, 131071]
Elapsed time = 00:00:00.000026760

Starting search for next Mp with known p = 17
[19, 2, 524287]
Elapsed time = 00:00:00.000022142

Starting search for next Mp with known p = 19
[31, 12, 2147483647]
Elapsed time = 00:00:00.000028463

Starting search for next Mp with known p = 31
[61, 30, 2305843009213693951]
Elapsed time = 00:00:00.000046226

Starting search for next Mp with known p = 61
[89, 28, 618970019642690137449562111]
Elapsed time = 00:00:00.000059962

Starting search for next Mp with known p = 89
[107, 18, 162259276829213363391578010288127]
Elapsed time = 00:00:00.000050865

Starting search for next Mp with known p = 107
[127, 20, 170141183460469231731687303715884105727]
Elapsed time = 00:00:00.000048701

Starting search for next Mp with known p = 127
[521, 394, 6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151]
Elapsed time = 00:00:00.002608888

Starting search for next Mp with known p = 521
[607, 86, 531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502265229285668889329486246501015346579337652707239409519978766587351943831270835393219031728127]
Elapsed time = 00:00:00.002203116

Starting search for next Mp with known p = 607
Unhandled exception: Arithmetic overflow (OverflowError)
  from /home/jzakiya/crystal/share/crystal/src/crystal/system/unix/time.cr:44:35 in '__crystal_main'
  from /home/jzakiya/crystal/share/crystal/src/crystal/at_exit_handlers.cr:21:26 in 'main'
  from /home/jzakiya/crystal/share/crystal/src/crystal/system/unix/main.cr:10:3 in 'main'
  from /lib/x86_64-linux-gnu/libc.so.6 in '??'
  from /lib/x86_64-linux-gnu/libc.so.6 in '__libc_start_main'
  from ./mpgs1 in '_start'
  from ???
➜  mps 

You see it only stopped because of the limitations of the software to continue processing the large numbers using p = 607. So now the algorithm|code is no longer time limited, but is software limited. If that can be resolved in Crystal I can find a new largest Mp by next week, since it’s now so fast. :grinning_face_with_smiling_eyes:

Hi Jabari,

I decided to help you a little and to improve the performance for your Crystal version.

I (together with my GPT 5.1 Sol + Luna agents) built a faster version of Mersenne Primes Search.
It can be found at GitHub - skuznetsov/mersenne-crystal-lab: Reproducible Crystal CPU/Metal experiments for Mersenne small-factor screening, with matched GMP baselines. · GitHub
It has CPU, Metal and CUDA versions. CPU and Metal versions were validated, CUDA has to be validated on Nvidia video cards.

CPU speed is 1.49 times faster than your last version you just posted.

More details can be found @ mersenne-crystal-lab/reply.md at main · skuznetsov/mersenne-crystal-lab · GitHub

Thanks @ComputerMage, I’ll get back to you on this.

I found (what I suspected) was the cause of the arithmetic overflow, and fixed it.

Do this replacement:

old: p = (Math.log(mp + 1)/Math.log(2)).to_u64

new: p = p + m  

I no longer get any overflows. Here’s a sample output I’ve gotten (so far) since I started running it with this change.

Starting search for next Mp with known p = 9941
[11213, 1272, 2814112013697373133393152975842584191818662382013600787892419349345515176682276313810715094745633257074198789308535071537342445016418881801789390548709414391857257571565758706478418356747070674633497188053050875416821624325680555826071110691946607460873056965360830571590242774934226866183966309185433462514537484258655982386235046029227507801410907163348439547781093397260096909677091843944555754221115477343760206979650067087884993478012977277878532807432236554020931571802310429923167588432457036104110850960439769038450365514022349625383665751207169661697352732236111926846454751701734527011379148175107820821297628946795631098960767492250494834254073334414121627833939461539212528932010726136689293688815665491671395174710452663709175753603774156855766515313827613727281696692633529666363787286539769941609107777183593336002680124517633451490439598324823836457251219406391432635639225604556042396004307799361927379900586400420763092320813392262492942076312933268033818471555255820639308889948665570202403815856313578949779767046261845327956725767289205262311752014786247813331834015084475386760526612217340579721237414485803725355463022009536301008145867524704604618862039093555206195328240951895107040793284825095462530151872823997171764140663315804309008611942578380931064748991594407476328437785848825423921170614938294029483257162979299388940695877375448948081108345293394327808452729789834135140193912419661799488795210328238112742218700634541149743657287232843426369348804878993471962403393967857676150371600196650252168250117793178488012000505422821362550520509209724459895852366827477851619190503254853115029403132178989005195751194301340277282730390683651120587895060198753121882187788657024007291784186518589977788510306743945896108645258766415692825664174470616153305144852273884549635059255410606458427323864109506687636314447514269094932953219924212594695157655009158521173420923275882063327625408617963032962033572563553604056097832111547535908988433816919747615817161606620557307000377194730013431815560750159027842164901422544571224546936793234970894954668425436412347785376194310030139080568383420772628618722646109707506566928102800033961704343991962002059794565527774913883237756792720065543768640792177441559278272350823092843683534396679150229676101834243787820420087274028617212684576388733605769491224109866592577360666241467280158988605523486345880882227855505706309276349415034547677180618296352866263005509222254318459768194126727603047460344175581029298320171226355234439676816309919127574206334807719021875413891580871529049187829308412133400910419756313021540478436604178446757738998632083586207992234085162634375406771169707323213988284943779122171985953605897902291781768286548287878180415060635460047164104095483777201737468873324068550430695826210304316336385311384093490021332372463463373977427405896673827544203128574874581960335232005637229319592369288171375276702260450911735069504025016667755214932073643654199488477010363909372005757899989580775775126621113057905717449417222016070530243916116705990451304256206318289297738303095152430549772239514964821601838628861446301936017710546777503109263030994747397618576207373447725441427135362428360863669327157635983045447971816718801639869547525146305655571843717916875669140320724978568586718527586602439602335283513944980064327030278104224144971883680541689784796267391476087696392191]
Elapsed time = 00:00:40.375117806

Here’s probably the last tweak to speed it up more. Someone in the Rust forum correctly pointed out only prime values of (p + m) need to be used. Here’s the modified code to do that, as well as it keeps the count of the number of (p + m) primes up to new Mp. This substantially decreases the search times now, and is functionally equivalent to GIMPS using candidates primes to test with, only here, we don’t need to create an external list first.

require "big"
require "primes-utils"  # provides ‘prime?'

p  = ARGV.size > 0 ? (ARGV[0].to_u64 underscore: true) : 3u64
m  = 2u64
tm = Time.instant
primes_tested = 0     # number of primes tested to new Mp
puts "Starting search for next Mp with known p = #{p}"

loop do
  until (p + m).prime?; m += 2 end
  primes_tested += 1
  mp = (1.to_big_i << (p + m)) - 1
  if mp.prime?
    p = p + m
    pp [p, m, primes_tested]
    puts "Elapsed time = #{Time.instant - tm }"
    puts "\nStarting search for next Mp with known p = #{p}\n"
    m  = 2u64
    tm = Time.instant
  else
    m += 2
  end
end

I think this is now the fastest MPG code. It cuts their times in more half for the larger values.
The ultimate fastest search implementation would be a multi-core parallel version.

Here’s an example of the performance increase compared to the previous version.

Starting search for next Mp with known p = 21701
[23209, 1508]
Elapsed time = 00:05:10.791830961

Starting search for next Mp with known p = 21701
[23209, 1508, 1365]
Elapsed time = 00:02:18.762170756

In the faithful performance of my duties as a human :face_with_raised_eyebrow: meat :meat_on_bone: proxy,
I submit data to the AI and copy and paste the code snippets it returns.

:meat_on_bone:

     puts "\nStarting search for next Mp with known p = #{p}\n"
     m = 2u64
+    primes_tested = 0
     tm = Time.instant
   else
     m += 2
   end

:meat_on_bone: :meat_on_bone:

# Without a precomputed prime list

require "big"
require "primes-utils"

q = 33_u64

loop do
  if q.prime? && ((1.to_big_i << q) - 1).prime?
    puts q
    break
  end
  q += 2
end

I caught resetting primes_tested = 0 almost as soon as I posted the upgrade. Without resetting it, it keeps a running tally of all the primes tested, and doesn’t affect the results for the Mp.

I have no idea what’s behind your q value. Its a composite, and can’t produce strings of 1 bits, so I’m very dubious about it. Have you run the code with that, and does it produce correct answers?

I’m afraid so.

I just updated the paper to explain|show how to do a better than brute force search.

Basically, you start the search with some large m value, then increment it with a value > 2. The paper explains everything, and includes the Crystal code that generates the Mp much more efficiently, and substantially faster, now.

require "big"
require "primes-utils"  # provides ‘prime?'

p       = ARGV.size > 0 ? (ARGV[0].to_u64 underscore: true) : 3u64
m_start = 1_200_u64
m_inc   = 16
m       = m_start
primes_tested = 0  # number of primes tested to new Mp

tm = Time.instant
puts "Starting search for next Mp with known p = #{p}"

loop do
  until (p + m).prime?; m += 2 end     # increment m to next prime
  primes_tested += 1
  mp = (1.to_big_i << (p + m)) - 1
  if mp.prime?
    p = p + m
    pp [p, m, primes_tested]
    puts "Elapsed time = #{Time.instant - tm }"
    puts "\nStarting search for next Mp with known p = #{p}\n"
    m, primes_tested = m_start, 0
    tm = Time.instant
  else
    m += m_inc
  end
end

This code allows you to play with different search strategies.

# Single core example to do an Optimal Mersenne Primes Search using Modular Groups
# crystal build --release --mcpu=native mpgs-single.cr
# $ ./mpgs-single p  m_start  m_inc
# $ ./mpps-single p  m_start  m_inc  0   # to display the tested prime values
# updated: 2026/10/15

require "big"
require "primes-utils"  # provides ‘prime?'

p       = ARGV[0].to_u64 underscore: true
m_start = ARGV[1].to_u64 underscore: true
m_inc   = ARGV[2].to_u64
show_p  = ARGV.size > 3
m       = m_start
primes_tested = 0  # number of primes tested to new Mp

tm = Time.instant
puts "Starting search for next Mp with known p = #{p}"

loop do
  until (p + m).prime?; m += 2 end     # increment m to next prime
  primes_tested += 1
  puts "Tested prime #{primes_tested}: m = #{m}; p = #{p + m}" if show_p
  mp = (1.to_big_i << (p + m)) - 1
  if mp.prime?
    p = p + m
    pp [p, m, primes_tested]
    puts "Elapsed time = #{Time.instant - tm }"
    exit
  end
  m += m_inc
end
-------------------------------------------------

➜  mps ./mpgs-single 9941 1200 16
Starting search for next Mp with known p = 9941
[11213, 1272, 4]
Elapsed time = 00:00:01.325268521

➜  mps ./mpgs-single 9941 1200 16 0
Starting search for next Mp with known p = 9941
Tested prime 1: m = 1208; p = 11149
Tested prime 2: m = 1230; p = 11171
Tested prime 3: m = 1256; p = 11197
Tested prime 4: m = 1272; p = 11213
[11213, 1272, 4]
Elapsed time = 00:00:01.334028274

This is now a better search strategy than used by GIMPS which is just a brute force search trying all the primes between each Mp value until its found.

  • The paper’s search strategy starts from the exponent p of a known Mersenne prime M_p = 2^p - 1. With m_start = 1200, it tests the first prime exponent q at or above p + 1200. If the test fails, m_inc = 16 advances the search to the first prime exponent at or above q + 16. The intervening candidates are not tested.
  • The paper motivates this strategy using the ratio between the gaps separating known Mersenne prime exponents and the number of prime exponents within those gaps. However, this ratio does not establish that the retained candidates are more likely to produce Mersenne primes than the skipped candidates.
  • Starting from p = 23209 with the same settings, the candidate sequence advances from 44483 to 44501, skipping the exponent of the next Mersenne prime, M_44497. Retaining a successful candidate may shorten the search, while skipping one results in a missed discovery opportunity. The reported examples do not distinguish favorable outcomes in individual cases from a systematic improvement in discovery efficiency, nor do they establish an advantage over GIMPS under equal computational resources.

@kojix2 Please do me a favor, don’t post AI slop on my work here. I would encourage you to take whatever time it may require, to study the paper, run examples yourself, and develop your own learned assessment of it, and even suggest (or better provide code) to make it better.

I (we all) were born with a brain that we can develop. I don’t use “artificial” intelligence when I was born with (and developed) my natural “human” intelligence (HI).

I appreciate your interest, and encourage you use your HI to understand the work, than AI to distort it.

@jzakiya
While learning is the process of using one’s own mind to walk around fields that have already been discovered, research is the exploration of fields where no one has ever entered before. Learning can bring happiness to one’s life, but research, in which the smartest people compete, usually requires the best tools available at the time, whatever they may be. Because others are using them, too.
If you claim to have discovered a new, efficient algorithm, it must at least stand up to review by tools these days, unfortunately.

FYI.

My code used a file primes-utils. I’ve made it a gist file that anyone can download|copy. I may someday make it a shard, but this is the easiest way (for me) to make it available to everyone.

primes-utils.cr: Primes-Utils Crystal version · GitHub

Place it in directory with the other source files, do in code: require "./primes-utils.cr"

The PRIMES-UTILS HANDBOOK (2016 Ruby version, which I’m updating) explains what it does.

https://www.academia.edu/19786419/PRIMES_UTILS_HANDBOOK