Mersenne primes: Difference between revisions

Content added Content deleted
(→‎{{header|ALGOL W}}: Can check 2^31 - 1 without overflow)
(→‎{{header|ALGOL W}}: Simplify and avoid overflow)
Line 68: Line 68:
for i := 3 step 2 until entier( sqrt( n ) ) do begin;
for i := 3 step 2 until entier( sqrt( n ) ) do begin;
isPrime := n rem i not = 0;
isPrime := n rem i not = 0;

if not isPrime then goto endPrimalityTest
if not isPrime then goto endPrimalityTest
end for_i;
end for_i;
Line 74: Line 75:


integer p2, n;
integer p2, n;
n := 1;
n := 2; % 2^2 is a special case %
p2 := 4;
p2 := 2;
while
if oddOnlyPrimalityTest( p2 - 1 ) then writeon( i_w := 1, s_w := 0, " ", n );
n := n + 1;
begin
p2 := p2 * 2;
if n < 3 then begin
while n <= 29 do begin % odd powers of two up to 29 %
n := n + 1;
if oddOnlyPrimalityTest( p2 - 1 ) then writeon( i_w := 1, s_w := 0, " ", n );
p2 := p2 * 2
n := n + 2;
end
if n <= 31 then p2 := p2 * 4
else begin
n := n + 2;
end while_n_le_31 ;
p2 := p2 * 4
end if_n_le_3__ ;;
if oddOnlyPrimalityTest( p2 - 1 ) then writeon( i_w := 1, s_w := 0, " ", n );
n < 29
end
do begin end ;
% MAXINTEGER is 2**31 - 1 %
% MAXINTEGER is 2**31 - 1 %
if oddOnlyPrimalityTest( MAXINTEGER ) then writeon( i_w := 1, s_w := 0, " ", 31 );
if oddOnlyPrimalityTest( MAXINTEGER ) then writeon( i_w := 1, s_w := 0, " ", 31 );