Find prime n such that reversed n is also prime: Difference between revisions

Content added Content deleted
(→‎{{header|RPL}}: straightforward approach)
Line 1,331: Line 1,331:
This program uses the words <code>RVSTR</code> and <code>BPRIM?</code>, respectively made to [[Reverse_a_string#RPL|revert a string]] and to [[Primality_by_trial_division#RPL|test primality by trial division]]
This program uses the words <code>RVSTR</code> and <code>BPRIM?</code>, respectively made to [[Reverse_a_string#RPL|revert a string]] and to [[Primality_by_trial_division#RPL|test primality by trial division]]
≪ { } 1 499 '''FOR''' n
≪ { } 1 499 '''FOR''' n
'''IF''' n R→B '''BPRIM? THEN'''
'''IF''' n R→B '<span style="color:blue">BPRIM?</span> THEN'''
'''IF''' n →STR '''RVSTR''' STR→ R→B '''BPRIM? THEN''' n + '''END'''
'''IF''' n →STR <span style="color:blue">RVSTR</span> STR→ R→B <span style="color:blue">BPRIM?</span> '''THEN''' n + '''END'''
'''END'''
'''END'''
'''NEXT'''
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
≫ EVAL

====Straightforward approach====
Numbers are here reversed without any string conversion.
{{works with|HP|49}}
« { }
1 499 '''FOR''' n
'''IF''' n ISPRIME? '''THEN'''
n 0
'''WHILE''' OVER '''REPEAT'''
SWAP 10 IDIV2 ROT 10 * +
'''END''' NIP
'''IF''' ISPRIME? '''THEN''' n + '''END'''
'''END'''
'''NEXT'''
» '<span style="color:blue">TASK</span>' STO
{{out}}
{{out}}
<pre>
<pre>