Ascending primes: Difference between revisions

mNo edit summary
Line 1,541:
235789 245789 345679 345689 1234789
1235789 1245689 1456789 12356789 23456789</pre>
 
=={{header|RPL}}==
Thanks to recursion, we have here a compact and efficient code that generates only ascending odd integers with 9 digits and less, and then check their primality.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ IF DUP 5 ≤ THEN { 2 3 5 } SWAP POS
ELSE
IF DUP 2 MOD NOT THEN 2
ELSE
DUP √ CEIL → lim
≪ 3 WHILE DUP2 MOD OVER lim ≤ AND REPEAT 2 + END
END MOD
END SIGN
≫ 'P'''RIM?'''' STO
SWAP 1 - SWAP
10 * LAST MOD 1 +
'''IF''' 3 PICK NOT '''THEN''' DUP 2 MOD NOT + '''END'''
+ LAST DROP 9 4 PICK - + '''FOR''' d
'''IF''' DUP
'''THEN''' SWAP OVER d '''APRIM''' SWAP
'''ELSE'''
IF d '''PRIM?''' '''THEN''' SWAP d + SWAP '''END'''
d 1 + 'd' STO
'''END'''
'''NEXT''' DROP
≫ ''''APRIM'''' STO
|
'''PRIM?''' ''( n -- boolean)''
'''APRIM''' ''( { } n seed -- { asc } )''
n ← n-1
preparing loop from ##u to ##v, where ## = seed,
u = last digit of seed + 1 (or + 2 if last recursion
and seed odd) ; v = 10 - n
if not last recursion
generate next digits
else
store in list if prime
skip to next odd value
.
forget n
.
|}
 
{{in}}
<pre>
≪ { 2 } 1 9 FOR n n 0 APRIM NEXT ≫ EVAL
</pre>
{{out}}
<pre>
1: { 2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789 }
</pre>
 
=={{header|Ruby}}==
Line 1,557 ⟶ 1,625:
<pre>2,3,5,7,13,17,19,23,29,37,47,59,67,79,89,127,137,139,149,157,167,179,239,257,269,347,349,359,367,379,389,457,467,479,569,1237,1249,1259,1279,1289,1367,1459,1489,1567,1579,1789,2347,2357,2389,2459,2467,2579,2689,2789,3457,3467,3469,4567,4679,4789,5689,12347,12379,12457,12479,12569,12589,12689,13457,13469,13567,13679,13789,15679,23459,23567,23689,23789,25679,34589,34679,123457,123479,124567,124679,125789,134789,145679,234589,235679,235789,245789,345679,345689,1234789,1235789,1245689,1456789,12356789,23456789
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func primes_with_ascending_digits(base = 10) {
1,150

edits