Primality by trial division: Difference between revisions

Content added Content deleted
Line 4,251: Line 4,251:
next
next
return 1</syntaxhighlight>
return 1</syntaxhighlight>

=={{header|RPL}}==
{{trans|Python}}
This version handles 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 → maxd
≪ #2 SWAP #5
'''WHILE''' DUP2 '''BDIV?''' NOT OVER maxd ≤ AND
'''REPEAT''' 3 PICK + #6 ROT - SWAP '''END'''
'''BDIV?''' NOT SWAP DROP
'''END'''
'''END'''
≫ ''''BPRIM?'''' STO
|
''( a b -- boolean )''
''( a -- boolean )''
return 1 if a is 2 or 3
if 2 or 3 divides a
return 0
else
store root(a)
initialize stack with i a 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
|}
{{in}}


=={{header|Ruby}}==
=={{header|Ruby}}==