Mersenne primes: Difference between revisions
→{{header|ALGOL W}}: Can check 2^31 - 1 without overflow
(→{{header|ALGOL W}}: Avoid overflow) |
(→{{header|ALGOL W}}: Can check 2^31 - 1 without overflow) |
||
Line 56:
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % find some Mersenne Primrs -
% integers are 32 bit in Algol W, so there won't be many numbers to check %
% we handle 2 as a special case and then the odd numbers starting at 3 %
Line 74:
integer p2, n;
n := 2; % 2^2 is a special case %
p2 := 4;
if oddOnlyPrimalityTest( p2 - 1 ) then writeon( i_w := 1, s_w := 0, " ", n );
n := n + 1;
p2 := p2 * 2;
while n <= 29 do begin % odd powers of two up to 29 %
if oddOnlyPrimalityTest( p2 - 1 ) then writeon( i_w := 1, s_w := 0, " ", n );
n := n + 2;
if n <= 31 then p2 := p2 * 4
end while_n_le_31 ;
% MAXINTEGER is 2**31 - 1 %
if oddOnlyPrimalityTest( MAXINTEGER ) then writeon( i_w := 1, s_w := 0, " ", 31 );
end.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 13 17 19 31
</pre>
|