primes

my code prints mersenne primes

2*2 - 1 = 3.

2*2*2 - 1 = 7.

etc

can anyone show me a better way of doing this code, something a little more efficient?

thanks

class MersennePrimes{

publicstaticvoid main(String[] args){

for(int num = 1; num <9000; num++)

if( test(num) ) System.out.print(num+" ");

System.out.print("are Mersenne Primes.");

}

staticboolean test(int i){

int sum = 1, count = 0;

while(sum < i){

sum = sum * 2;

count = count +1;

}

int sumMinusOne = sum -1;

if (PrimeTwinPairs.isPrime(sumMinusOne) && PrimeTwinPairs.isPrime(count)

&& sumMinusOne == i )

returntrue;

elsereturnfalse;

}

}

[1666 byte] By [mark_8206a] at [2007-11-27 9:46:19]
# 1
A faster way of generating Mersenne Primes is by implementing the Lucas-Lehmer algorithm. It's even the fastest (up to this day), if I'm not mistaken. Use Google to find out more.Good luck.
prometheuzza at 2007-7-12 23:56:38 > top of Java-index,Java Essentials,New To Java...
# 2
thanks prome
mark_8206a at 2007-7-12 23:56:38 > top of Java-index,Java Essentials,New To Java...
# 3
> thanks promeYou're welcome mark.
prometheuzza at 2007-7-12 23:56:38 > top of Java-index,Java Essentials,New To Java...
# 4

Quote of the day:

"There are two facts about the distribution of prime numbers of which I hope to convince you so overwhelmingly that they will be permanently engraved in your hearts. The first is that, despite their simple definition and role as the building blocks of the natural numbers, the prime numbers grow like weeds among the natural numbers, seeming to obey no other law than that of chance, and nobody can predict where the next one will sprout. The second fact is even more astonishing, for it states just the opposite: that the prime numbers exhibit stunning regularity, that there are laws governing their behavior, and that they obey these laws with almost military precision."

http://en.wikipedia.org/wiki/Prime_number

BigDaddyLoveHandlesa at 2007-7-12 23:56:38 > top of Java-index,Java Essentials,New To Java...