Jump to content

Smarandache prime-digital sequence: Difference between revisions

Added Forth solution
(add freebasic)
(Added Forth solution)
Line 526:
10000th member: 273322727
100000th member: 23325232253
</pre>
 
=={{header|Forth}}==
<lang forth>: is_prime? ( n -- flag )
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
dup 3 mod 0= if 3 = exit then
5
begin
2dup dup * >=
while
2dup mod 0= if 2drop false exit then
2 +
2dup mod 0= if 2drop false exit then
4 +
repeat
2drop true ;
 
: next_prime_digit_number ( n -- n )
dup 0= if drop 2 exit then
dup 10 mod
dup 2 = if drop 1+ exit then
dup 3 = if drop 2 + exit then
dup 5 = if drop 2 + exit then
drop
10 / recurse 10 * 2 + ;
 
: spds_print ( n -- )
0
begin
over 0 >
while
next_prime_digit_number
dup is_prime? if dup . swap 1- swap then
repeat
2drop cr ;
 
: spds_nth ( n -- n )
0
begin
over 0 >
while
next_prime_digit_number
dup is_prime? if swap 1- swap then
repeat
swap drop ;
 
." First 25 SPDS primes:" cr
25 spds_print
 
." 100th SPDS prime: "
100 spds_nth . cr
 
." 1000th SPDS prime: "
1000 spds_nth . cr</lang>
 
{{out}}
<pre>
First 25 SPDS primes:
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
100th SPDS prime: 33223
1000th SPDS prime: 3273527
</pre>
 
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.