Smarandache prime-digital sequence: Difference between revisions
Content added Content deleted
No edit summary |
(add RPL) |
||
Line 1,977: | Line 1,977: | ||
</pre> |
</pre> |
||
=={{header|RPL}}== |
|||
Brute force being not an option for the slow machines that can run RPL, optimisation is based on a prime-digital number generator returning possibly prime numbers, e.g. not ending by 2 or 5. |
|||
<code>PRIM?</code> is defined at [[Primality by trial division#RPL|Primality by trial division]]. |
|||
{| class="wikitable" |
|||
! RPL code |
|||
! Comment |
|||
|- |
|||
| |
|||
≪ { "2" "3" "5" "7" } DUP SIZE → digits base |
|||
≪ DUP SIZE 1 CF 2 SF |
|||
'''DO''' |
|||
'''IF''' DUP NOT '''THEN''' digits 1 GET ROT + SWAP 1 CF |
|||
'''ELSE''' |
|||
DUP2 DUP SUB digits SWAP POS |
|||
'''IF''' 2 FS?C '''THEN''' 2 == 4 ≪ 1 SF 2 ≫ IFTE |
|||
'''ELSE IF''' DUP base == '''THEN''' |
|||
SIGN 1 SF '''ELSE''' 1 + 1 CF '''END''' |
|||
'''END''' |
|||
digits SWAP GET REPL |
|||
LASTARG ROT DROP2 1 - '''END''' |
|||
'''UNTIL''' 1 FC? '''END''' DROP |
|||
≫ ≫ ‘<span style="color:blue">'''NSPDP'''</span>’ STO |
|||
| |
|||
<span style="color:blue">'''NSPDP'''</span> ''( "PDnumber" → "nextPDnumber" )'' |
|||
rank = units position, carry = 0, 1st pass = true |
|||
Loop |
|||
If rank = 0 then add a new digit rank |
|||
Else |
|||
digit = ord[rank] |
|||
If first pass then next digit = 3 or 7 |
|||
Else if digit is the last of the series |
|||
Then next digit = 1 otherwise = ++digit |
|||
Replace digit with next_digit |
|||
Rank-- |
|||
Until no carry |
|||
return number as a string |
|||
|} |
|||
≪ { 2 3 5 } 7 |
|||
'''WHILE''' OVER SIZE 100 < '''REPEAT''' |
|||
'''IF''' DUP <span style="color:blue">'''PRIM?'''</span> '''THEN''' SWAP OVER + SWAP '''END''' |
|||
STR→ <span style="color:blue">'''NSPDP'''</span> STR→ '''END''' DROP ≫ EVAL |
|||
DUP 1 25 SUB |
|||
SWAP 100 GET |
|||
{{out}} |
|||
<pre> |
|||
2: { 2 3 5 7 23 37 53 73 223 227 233 257 277 337 353 373 523 557 577 727 733 757 773 2237 2273 } |
|||
1: 33223 |
|||
</pre> |
|||
task needs 3 min 45 s to run on a HP-48G. |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<syntaxhighlight lang="rust">fn is_prime(n: u32) -> bool { |
<syntaxhighlight lang="rust">fn is_prime(n: u32) -> bool { |