Lucas-Lehmer Test for Mersenne Primes

  • Context: Graduate 
  • Thread starter Thread starter Zurtex
  • Start date Start date
  • Tags Tags
    Test
Click For Summary

Discussion Overview

The discussion revolves around the implementation of the Lucas-Lehmer Test for identifying Mersenne primes using MATLAB. Participants share their programming challenges, solutions, and suggestions for improving efficiency in the context of prime number testing.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes their initial brute force approach to finding Mersenne primes up to 2^2281 - 1 and expresses difficulty in implementing the Lucas-Lehmer Test in MATLAB.
  • Another participant identifies a potential mistake in the original code and suggests leaving the thread open for further feedback.
  • A later post indicates that the participant has modified their code to work for Mersenne primes up to 2^19 - 1 but encounters issues with larger values.
  • One participant proposes that MATLAB's double precision may be limiting the calculations, suggesting that intermediate results should be reduced modulo 2^p - 1 to avoid overflow.
  • Another participant mentions having found a solution using Maple, which handles symbolic input better, but notes that it is not as fast as desired.
  • A suggestion is made to explore the GIMPS project for faster algorithms and additional resources related to Mersenne primes.

Areas of Agreement / Disagreement

Participants express differing views on the limitations of MATLAB for large integer calculations, with some suggesting alternative software while others focus on improving MATLAB code. There is no consensus on the best approach to resolve the programming issues.

Contextual Notes

Participants mention limitations related to MATLAB's data types and precision, as well as the need for efficient algorithms when dealing with large numbers in the context of prime testing.

Who May Find This Useful

Individuals interested in programming algorithms for prime number testing, particularly those using MATLAB or exploring the Lucas-Lehmer Test and Mersenne primes.

Zurtex
Science Advisor
Homework Helper
Messages
1,118
Reaction score
1
I'm trying to come up with a program that works out Mersenne prime numbers, I've successfully managed to get one to work that brute force attacks the problem and gets me up to 2^2281 - 1 as about the highest prime I can get with the computer still running fine.

I've been reading up and it seems one of the best ways is to use the Lucas-Lehmer Test (http://mathworld.wolfram.com/Lucas-LehmerTest.html) however I am fairly new to MATLAB and don't quite seem to get it to work. Could anyone give me some points where I am going wrong please.

(Below is code I have used to test if 2^p - 1 is prime or not)
(z records the biggest value of p such that 2^p - 1 is prime)
Code:
p=sym(2)

for p=2:100
    if issymprime(p)==sym('true')
        s = 4
        for s=4:(p-2)
            s = mod(s^2 - 2,2^p -1)
        end
        if s==mod(0,2^p-1)
            z=p
        end
    end
end
 
Mathematics news on Phys.org
O.K just noticed a huge mistake in my programming, but can I leave the thread here for a sec in case it is still horribly wrong?
 
Really sorry for the triple post but I've got it to work with a slight problem:

Code:
p=sym(2)

for p=2:40
    if issymprime(p)==sym('true')
        s = 1
        r = 4
        for s=1:(p-2)
            r = mod(r^2 - 2,2^p -1)
        end
        if r==mod(0,2^p-1)
            z=p
        end
    end
end

That works, up to about 2^19 - 1, however after that it doesn't seem to pick anymore Mersenne prime numbers up. Could someone tell me what to do please.
 
Last edited:
Hi, I think your problem is with how MATLAB stores data, it uses double precision variables, which are 64-bits and accurate to 15 decimal places or so. When computing your r's for p=31, at one stage you calculate 1416317953^2-2, which has 18 digits, before reducing mod 2^31-1. This is where your program bites it.

You can probably have it compute 1416317953^2-2 in pieces, reducing mod 2^31-1 each time. There is also an integer type, but I believe it only goes to 32 bits, not enough for you.

I don't have much experience with large integer operations in matlab, so I don't know if there's a clever way around this. On the other hand, Maple is quite capable of handling things like this.
 
Thanks, I thought that was my problem and managed to come up with a maple solution. My lecturer has already made some functions for us that can handle symbolic input including a round() function which seems to work great and does so by using a couple of maple functions :smile:

Anyway it isn't quite as fast as I hoped but I can probably change that over time when I get to know MATLAB a bit better.
 
Look at GIMPS

Hi,
You should have a look at the GIMPS project: http://www.mersenne.org/ .
You'll find useful information and a very fast program on ia32/Windows machines.
You could also use the GLucas program for Unix machines. It is multi-threaded.
About the theory, look at Ribenboim's book: "The little book of bigger primes" or at Williams' book: "Edouard Lucas and Primality Testing".
Does Mathlab use FFT for multiplying big numbers ?
Regards,
Tony
 
Thanks, with time I could work a much faster algorithm but I had to submit what I had today.

unfortunately I have little understanding of how MatLab works at the moment, but I will look into it.
 

Similar threads

  • · Replies 24 ·
Replies
24
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 96 ·
4
Replies
96
Views
12K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K