SnTT: Mersenne Primes
Roughly 9 years ago, a contractor (who I never met) at the company where I worked at the time was fired for installing a grid program on a bunch of company computers in the hopes of discovering a new prime number. It brought the network to its knees. I don't know what reminded me of that, but I started thinking about how one would go about testing a number's primality in LotusScript. For low numbers, it's quite easy:
Let PotentialPrime = (2 ^ Exponent) - 1
Let IsPrime = True 'Assume prime until proven otherwise
For Divisor = 2 To Fix(dblIsPrime / 2)
If (PotentialPrime Mod Divisor = 0) Then
Let IsPrime = False
Exit For
End If
Next
Of course, LotusScript has pretty low limits to number storage... even when using Doubles, you get an Overflow pretty rapidly. Just for fun, I did a little reading and stumbled upon the concept of Mersenne Primes: basically, since all prime numbers higher than 2 are odd, 2n - 1 tends to be prime if n is also prime. But not always... so the "Lucas-Lehmer test" is a formula for determining whether or not a number conforming to this pattern is in fact prime. I found a Java implementation of that formula, pulled it into a script library in a Notes database, modified the main function to make it a static boolean instead of running as a command line program, then wrote a background thread agent to test exponents and create a document for any that produce a valid Mersenne:
import lotus.domino.*;
import java.math.BigInteger; // for determining probable primality of exponents
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Database dbCurrent = agentContext.getCurrentDatabase();
View vwPrimes = dbCurrent.getView("Primes");
Document docLastFound = vwPrimes.getFirstDocument(); // pick up where we left off last time
int intExponent = docLastFound.getItemValueInteger("Exponent") + 2; // only works for odd exponents
int intPrimesFound = 0;
while (intPrimesFound < 128) { // only 44 have been found so far, so this could take a while
BigInteger possiblePrime = new BigInteger(new Integer(intExponent).toString());
if (possiblePrime.isProbablePrime(1)) { // don't bother checking an exponent if it isn't prime to begin with
if (LucasLehmer.isPrime(intExponent)) {
docLastFound = dbCurrent.createDocument();
docLastFound.replaceItemValue("Form","Prime");
docLastFound.replaceItemValue("Exponent",new Integer(intExponent)); // Notes won't store an int
docLastFound.save(true,true,true);
intPrimesFound++;
System.out.println("Lucas approves of exponent " + intExponent);
}
}
intExponent += 2; // grab the next odd exponent
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
It's already found 18 of the 44 known Mersenne primes.
Comments
Posted by Vitor Pereira At 11:39:55 AM On 07/05/2007 | - Website - |
Posted by Tim Tripcony At 03:06:38 PM On 07/05/2007 | - Website - |
Posted by Chris Whisonant At 03:55:09 PM On 07/05/2007 | - Website - |