Mersenne primes: Difference between revisions
Content added Content deleted
(→{{header|ALGOL W}}: Simplify and avoid overflow) |
|||
Line 1,644: | Line 1,644: | ||
17 |
17 |
||
19 |
19 |
||
</pre> |
|||
=={{header|RPL}}== |
|||
This version use binary integers |
|||
{{works with|Halcyon Calc|4.2.7}} |
|||
{| class="wikitable" |
|||
! RPL code |
|||
! Comment |
|||
|- |
|||
| |
|||
≪ / LAST ROT * - #0 == ≫ ''''BDIV?'''' STO |
|||
≪ |
|||
'''IF''' DUP #3 ≤ '''THEN''' #2 / B→R |
|||
'''ELSE''' |
|||
'''IF''' DUP #2 '''BDIV?''' OVER #3 '''BDIV?''' OR |
|||
'''THEN''' DROP 0 |
|||
'''ELSE''' |
|||
DUP B→R √ R→B → a maxd |
|||
≪ a #2 #5 |
|||
'''WHILE''' a OVER '''BDIV?''' NOT OVER maxd ≤ AND |
|||
'''REPEAT''' OVER + #6 ROT - SWAP '''END''' |
|||
≫ |
|||
SWAP DROP '''BDIV?''' NOT |
|||
'''END''' |
|||
'''END''' |
|||
≫ ''''BPRIM?'''' STO |
|||
≪ {} 1 32 '''FOR''' n |
|||
#1 1 n '''START''' SL '''NEXT''' #1 - |
|||
'''IF BPRIM? THEN''' + '''END''' |
|||
'''NEXT''' |
|||
≫ EVAL |
|||
| |
|||
''( #a #b -- boolean )'' |
|||
''( #a -- boolean )'' |
|||
return 1 if a is 2 or 3 |
|||
if 2 or 3 divides a |
|||
return 0 |
|||
else |
|||
store a and root(a) |
|||
initialize stack with a i d |
|||
while d does not divide a and d <= root(a) |
|||
i = 6 - i which modifies 2 into 4 and viceversa |
|||
convert stack status into result |
|||
Let's loop |
|||
Generate M(n) |
|||
test primality |
|||
|} |
|||
{{out}} |
|||
<pre> |
|||
{ 2 3 5 7 13 17 19 31 } |
|||
</pre> |
</pre> |
||