Descending primes: Difference between revisions

Added a Forth implementation of the task
(Descending primes in FreeBASIC)
(Added a Forth implementation of the task)
Line 244:
There are 87 descending primes.</pre>
 
=={{header|Forth}}==
Tested on vfxforth and GForth.
 
<lang forth>: is-prime? \ n -- f ; \ Fast enough for this application
DUP 1 AND IF \ n is odd
DUP 3 DO
DUP I DUP * < IF DROP -1 LEAVE THEN \ Leave loop if I**2 > n
DUP I MOD 0= IF DROP 0 LEAVE THEN \ Leave loop if n%I is zero
2 +LOOP \ iterate over odd I only
ELSE \ n is even
2 = \ Returns true if n == 2.
THEN ;
 
: 1digit \ -- ; \ Select and print one digit numbers which are prime
10 2 ?DO
I is-prime? IF I 9 .r THEN
LOOP ;
 
: 2digit \ n-bfwd digit -- ;
\ Generate and print primes where least significant digit < digit
\ n-bfwd is the base number bought foward from calls to `digits` below.
SWAP 10 * SWAP 1 ?DO
DUP I + is-prime? IF DUP I + 9 .r THEN
2 I 3 = 2* - +LOOP DROP ; \ This generates the I sequence 1 3 7 9
 
: digits \ #digits n-bfwd max-digit -- ;
\ Print descendimg primes with #digits digits.
2 PICK 9 > IF ." #digits must be less than 10." 2DROP DROP EXIT THEN
2 PICK 1 = IF 2DROP DROP 1digit EXIT THEN \ One digit is special simple case
2 PICK 2 = IF \ Two digit special and
SWAP 10 * SWAP 2 DO \ I is 2 .. max-digit-1
DUP I + I 2digit
LOOP 2DROP
ELSE
SWAP 10 * SWAP 2 PICK ?DO \ I is #digits .. max-digit-1
DUP I + 2 PICK 1- SWAP I RECURSE \ Recurse with #digits reduced by 1.
LOOP 2DROP
THEN ;
 
: descending-primes
\ Print the descending primes. Call digits with increasing #digits
CR 9 1 DO I 0 10 digits LOOP ;</lang>
<pre>
descending-primes
2 3 5 7 31 41 43 53 61 71
73 83 97 421 431 521 541 631 641 643
653 743 751 761 821 853 863 941 953 971
983 5431 6421 6521 7321 7541 7621 7643 8431 8521
8543 8641 8731 8741 8753 8761 9421 9431 9521 9631
9643 9721 9743 9851 9871 75431 76421 76541 76543 86531
87421 87541 87631 87641 87643 94321 96431 97651 98321 98543
98621 98641 98731 764321 865321 876431 975421 986543 987541 987631
8764321 8765321 9754321 9875321 97654321 98764321 98765431 ok
</pre>
 
=={{header|Go}}==
Anonymous user