Ascending primes: Difference between revisions

implementation in Forth
(implementation in Forth)
Line 436:
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
</pre>
 
 
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
<br>
<lang forth>#! /usr/bin/gforth
 
\ Ascending primes
 
\ checks (by simple trial-division) whether the TOS is prime
: prime? ( n -- f )
dup 1 <= IF
drop false
ELSE
dup 2 = IF
drop true
ELSE
2
BEGIN
2dup dup * > >r
2dup mod 0> r>
over and
WHILE
drop 1+
REPEAT
nip nip
THEN
THEN
;
 
: ascending-primes-aux ( n i -- )
dup 10 = IF
drop
dup prime? IF
.
ELSE
drop
THEN
ELSE
2dup 1+ recurse \ do not include digit i
swap 10 * over + swap 1+ recurse \ do include digit i
THEN
;
 
\ prints all primes with strictly ascending digits
: ascending-primes ( -- )
0 1 ascending-primes-aux cr
;
 
ascending-primes bye
</lang>
 
{{out}}
<pre>
./ascending-primes.fs
89 7 79 67 5 59 569 5689 47 479 4789 467 4679 457 4567 3 389 37 379 367 359 349 347 3469 3467 34679 34589 3457 345689 345679 2 29 2789 269 2689 257 2579 25679 2467 2459 245789 23 239 2389 23789 23689 2357 235789 23567 235679 2347 23459 234589 23456789 19 17 179 1789 167 157 1579 1567 15679 149 1489 1459 145679 1456789 13 139 137 13789 1367 13679 13567 134789 13469 13457 1289 127 1279 12689 1259 12589 125789 12569 1249 12479 124679 12457 1245689 124567 1237 12379 1235789 12356789 12347 123479 1234789 123457
</pre>
 
22

edits