Lucas-Lehmer Test for Mersenne Primes

  • Thread starter Zurtex
  • Start date
  • Tags
    Test
In summary, the speaker is trying to come up with a program to find Mersenne prime numbers. They have successfully created a brute force program, but are now interested in using the Lucas-Lehmer Test. They are new to MATLAB and are seeking help in implementing the test. They share their code and ask for feedback, and later realize a mistake in their programming. They also mention a potential issue with MATLAB's data storage and suggest using Maple instead. Another person suggests looking into the GIMPS project for a faster program and recommends some resources for understanding the theory behind Mersenne primes. The speaker notes their limited understanding of MATLAB and their plan to improve their algorithm over time.
  • #1
Zurtex
Science Advisor
Homework Helper
1,120
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
  • #2
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?
 
  • #3
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:
  • #4
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.
 
  • #5
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.
 
  • #6
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
 
  • #7
Thanks, with time I could work a much faster algorithm but I had to submit what I had today.

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

What is the Lucas-Lehmer Test for Mersenne Primes?

The Lucas-Lehmer Test is a mathematical algorithm used to determine if a number of the form 2n - 1 is a prime number. This test is specifically designed for Mersenne numbers, which are numbers of the form 2n - 1, where n is a prime number.

How does the Lucas-Lehmer Test work?

The Lucas-Lehmer Test works by using a specific formula to generate a sequence of numbers, known as the Lucas-Lehmer sequence. If the final number in the sequence is equal to 0, then the number being tested is a Mersenne prime. If the final number is not equal to 0, then the number is not a Mersenne prime and can be proven to be composite.

Why is the Lucas-Lehmer Test important?

The Lucas-Lehmer Test is important because it provides a relatively simple and efficient method for determining if a number is a Mersenne prime. Mersenne primes are significant in mathematics and computer science, as they have important applications in number theory and can also be used to generate large prime numbers for cryptographic purposes.

What is the time complexity of the Lucas-Lehmer Test?

The time complexity of the Lucas-Lehmer Test is O(log2n), which means that the time it takes to run the test increases logarithmically as the size of the number being tested increases. This makes it a relatively fast algorithm compared to other methods of determining primality.

Are there any limitations to the Lucas-Lehmer Test?

Yes, the Lucas-Lehmer Test can only be used to determine the primality of Mersenne numbers. It cannot be used for other types of numbers, such as non-Mersenne primes or composite numbers. Additionally, the test can only be used for numbers of the form 2n - 1, where n is a prime number. This means that not all prime numbers can be tested using this method.

Similar threads

  • General Math
Replies
24
Views
2K
  • Linear and Abstract Algebra
Replies
2
Views
810
Replies
1
Views
2K
  • Precalculus Mathematics Homework Help
Replies
13
Views
1K
  • Introductory Physics Homework Help
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
15
Views
2K
  • General Math
Replies
1
Views
1K
  • General Math
3
Replies
96
Views
10K
  • General Math
Replies
16
Views
3K
Replies
4
Views
687
Back
Top