Lucas-Lehmer test: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: python colorization, remove nonpythonic syntax)
Line 391: Line 391:


=={{header|Python}}==
=={{header|Python}}==
from sys import stdout
<python> from sys import stdout
from math import sqrt, log
from math import sqrt, log
fi = od = None
def is_prime ( p ):
def is_prime ( p ):
Line 399: Line 398:
elif p <= 1 or p % 2 == 0: return False
elif p <= 1 or p % 2 == 0: return False
else:
else:
for i in range(3, int(sqrt(p))+1, 2 ):
prime = True;
for i in range(3, int(sqrt(p))+1, 2 ): # do
if p % i == 0: return False
if p % i == 0: return False
else:
return True
return True
fi
fi;
def is_mersenne_prime ( p ):
def is_mersenne_prime ( p ):
if p == 2: return True
if p == 2:
return True
else:
else:
m_p = ( 1 << p ) - 1; s = 4;
m_p = ( 1 << p ) - 1;
for i in range(3, p+1): # do
s = 4;
for i in range(3, p+1):
s = (s ** 2 - 2) % m_p
s = (s ** 2 - 2) % m_p
od;
return s == 0
return s == 0
fi;
precision = 20000; # maximum requested number of decimal places of 2 ** MP-1 #
precision = 20000 # maximum requested number of decimal places of 2 ** MP-1 #
long_bits_width = precision / log(2) * log(10);
long_bits_width = precision / log(2) * log(10)
upb_prime = int( long_bits_width - 1 ) / 2; # no unsigned #
upb_prime = int( long_bits_width - 1 ) / 2 # no unsigned #
upb_count = 45; # find 45 mprimes if int was given enough bits #
upb_count = 45 # find 45 mprimes if int was given enough bits #
print ((" Finding Mersenne primes in M[2..%d]:"%upb_prime));
print (" Finding Mersenne primes in M[2..%d]:"%upb_prime)
count=0;
count=0
for p in range(2, upb_prime+1): # do
for p in range(2, upb_prime+1):
if is_prime(p) and is_mersenne_prime(p):
if is_prime(p) and is_mersenne_prime(p):
print("M%d"%p),
print("M%d"%p),
stdout.flush();
stdout.flush()
count += 1
count += 1
fi;
if count >= upb_count: break
if count >= upb_count: break
print</python>
od
print


Output:
Output:
Finding Mersenne primes in M[2..33218]:
<pre> Finding Mersenne primes in M[2..33218]:
M2 M3 M5 M7 M13 M17 M19 M31 M61 M89 M107 M127 M521 M607 M1279 M2203 M2281 M3217 M4253 M4423 M9689 M9941 M11213 M19937 M21701 M23209
M2 M3 M5 M7 M13 M17 M19 M31 M61 M89 M107 M127 M521 M607 M1279 M2203 M2281 M3217 M4253 M4423 M9689 M9941 M11213 M19937 M21701 M23209</pre>


=={{header|Scheme}}==
=={{header|Scheme}}==