Pascal's Weblog
The Grid...



Archives
« December 2009
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today
Click me to subscribe
Search

Links
 

Today's Page Hits: 34

« Grid server | Main | Distributed Mersenne... »
Monday Aug 14, 2006
Mersenne Primes
Mersenne primes are primes of the form: 2^p-1
Mersenne primes are found using the Lucas-Lehmer Test.

A simple Java implementation of this test taking advantage of the BigInteger class is the following:

import java.math.*;
public class Mersenne {

  static final BigInteger one  = new BigInteger("1");
  static final BigInteger two  = new BigInteger("2");
  static final BigInteger four = new BigInteger("4");

  private static boolean LucasLehmerTest(int p) {
    BigInteger s = four;
    BigInteger n = one.shiftLeft(p).subtract(one);
    for (int i = 3; i <= p; i++) {
      s = s.multiply(s).subtract(two).mod(n);
    }
    if (s.bitCount() == 0) {
      return true;
    } else {
      return false;
    }
  }

  public static void main(String[] args) {
    for (int i = 0; i < 5000; i++) {
      if (LucasLehmerTest(i)) {
        System.out.println("2^" + i + "-1 is prime");
      }
    }
  }

}

Ant the output will look like:
2^3-1 is prime
2^5-1 is prime
2^7-1 is prime
2^13-1 is prime
2^17-1 is prime
2^19-1 is prime
2^31-1 is prime
2^61-1 is prime
2^89-1 is prime
2^107-1 is prime
2^127-1 is prime
2^521-1 is prime
2^607-1 is prime
2^1279-1 is prime
2^2203-1 is prime
2^2281-1 is prime
2^3217-1 is prime
2^4253-1 is prime
2^4423-1 is prime

(Of course 2^2-1 is also prime!).
Well, this program does not exactly take advantage of the full power of the Grid (The run took 1.189 hour)! All the computation is done by just one CPU! Let see, if we can rewrite this program to take advantage of the Grid...
Posted at 01:43PM Aug 14, 2006 by Pascal Ledru in Grid Computing  |  Comments[0]

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed