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]

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