Smarandache prime-digital sequence: Difference between revisions

Added Easylang
m (C++ - renamed class)
(Added Easylang)
 
(40 intermediate revisions by 19 users not shown)
Line 15:
* https://www.scribd.com/document/214851583/On-the-Smarandache-prime-digital-subsequence-sequences
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F divisors(n)
V divs = [1]
L(ii) 2 .< Int(n ^ 0.5) + 3
I n % ii == 0
divs.append(ii)
divs.append(Int(n / ii))
divs.append(n)
R Array(Set(divs))
 
F is_prime(n)
R divisors(n).len == 2
 
F digit_check(n)
I String(n).len < 2
R 1B
E
L(digit) String(n)
I !is_prime(Int(digit))
R 0B
R 1B
 
F sequence(max_n)
V ii = 0
V n = 0
[Int] r
L
ii++
I is_prime(ii)
I n > max_n
L.break
I digit_check(ii)
n++
r.append(ii)
R r
 
V seq = sequence(100)
print(‘First 25 SPDS primes:’)
L(item) seq[0.<25]
print(item, end' ‘ ’)
print()
print(‘Hundredth SPDS prime: ’seq[99])</syntaxhighlight>
 
{{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
Hundredth SPDS prime: 33223
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
BYTE FUNC IsZero(REAL POINTER a)
CHAR ARRAY s(10)
 
StrR(a,s)
IF s(0)=1 AND s(1)='0 THEN
RETURN (1)
FI
RETURN (0)
 
CARD FUNC MyMod(CARD a,b)
REAL ar,br,dr
CARD d,m
 
IF a>32767 THEN
;Built-in DIV and MOD
;do not work properly
;for numbers greater than 32767
IntToReal(a,ar)
IntToReal(b,br)
RealDiv(ar,br,dr)
d=RealToInt(dr)
m=a-d*b
ELSE
m=a MOD b
FI
RETURN (m)
 
BYTE FUNC IsPrime(CARD a)
CARD i
 
IF a<=1 THEN
RETURN (0)
FI
i=2
WHILE i*i<=a
DO
IF MyMod(a,i)=0 THEN
RETURN (0)
FI
i==+1
OD
RETURN (1)
 
BYTE FUNC AllDigitsArePrime(CARD a)
BYTE i
CHAR ARRAY s
CHAR c
 
StrC(a,s)
FOR i=1 TO s(0)
DO
c=s(i)
IF c#'2 AND c#'3 AND c#'5 AND c#'7 THEN
RETURN (0)
FI
OD
RETURN (1)
 
PROC Main()
BYTE count
CARD a
 
Put(125) PutE() ;clear screen
PrintE("Sequence from 1st to 25th:")
count=0 a=1
DO
IF AllDigitsArePrime(a)=1 AND IsPrime(a)=1 THEN
count==+1
IF count<=25 THEN
PrintC(a) Put(32)
ELSEIF count=100 THEN
PrintF("%E%E100th: %U%E",a)
EXIT
FI
FI
a==+1
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Smarandache_prime-digital_sequence.png Screenshot from Atari 8-bit computer]
<pre>
Sequence from 1st to 25th:
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: 33223
</pre>
 
=={{header|ALGOL 68}}==
Uses a sieve to find primes. Requires --heap 256m for Algol 68G.
<br>Uses the optimisations of the Factor, Phix, etc. samples.
<langsyntaxhighlight lang="algol68"># find elements of the Smarandache prime-digital sequence - primes whose #
# digits are all primes #
# Uses the observations that the final digit of 2 or more digit Smarandache #
Line 120 ⟶ 264:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 129 ⟶ 273:
Largest Smarandache prime under 10000000: 7777753 (Smarandache prime 1903)
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">spds: 2..∞ | select.first:100 'x ->
and? -> prime? x
-> every? digits x => prime?
 
print "First 25 SPDS primes:"
print first.n: 25 spds
 
print ""
print ["100th SPDS prime:" last spds]</syntaxhighlight>
 
{{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</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SMARANDACHE_PRIME-DIGITAL_SEQUENCE.AWK
BEGIN {
Line 170 ⟶ 332:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 176 ⟶ 338:
100: 33223
</pre>
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="freebasic">arraybase 1
dim smar(100)
smar[1] = 2
 
cont = 1
i = 1
 
print 1, 2
while cont < 100
i += 2
if not isPrime(i) then continue while
for j = 1 to length(string(i))
digit = int(mid(string(i),j,1))
if not isPrime(digit) then continue while
next j
cont += 1
smar[cont] = i
if cont = 100 or cont <= 25 then print cont, smar[cont]
end while
end
 
function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
 
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <assertlocale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
 
typedef structuint32_t bit_array_tag {integer;
uint32_t size;
uint32_t* array;
} bit_array;
 
integer next_prime_digit_number(integer n) {
bool bit_array_create(bit_array* b, uint32_t size) {
uint32_t* array = calloc((size + 31)/32, sizeof(uint32_t));
if (array == NULL)
return false;
b->size = size;
b->array = array;
return true;
}
 
void bit_array_destroy(bit_array* b) {
free(b->array);
b->array = NULL;
}
 
void bit_array_set(bit_array* b, uint32_t index, bool value) {
assert(index < b->size);
uint32_t* p = &b->array[index >> 5];
uint32_t bit = 1 << (index & 31);
if (value)
*p |= bit;
else
*p &= ~bit;
}
 
bool bit_array_get(const bit_array* b, uint32_t index) {
assert(index < b->size);
uint32_t* p = &b->array[index >> 5];
uint32_t bit = 1 << (index & 31);
return (*p & bit) != 0;
}
 
typedef struct sieve_tag {
uint32_t limit;
bit_array not_prime;
} sieve;
 
bool sieve_create(sieve* s, uint32_t limit) {
if (!bit_array_create(&s->not_prime, limit + 1))
return false;
bit_array_set(&s->not_prime, 0, true);
bit_array_set(&s->not_prime, 1, true);
for (uint32_t p = 2; p * p <= limit; ++p) {
if (bit_array_get(&s->not_prime, p) == false) {
for (uint32_t q = p * p; q <= limit; q += p)
bit_array_set(&s->not_prime, q, true);
}
}
s->limit = limit;
return true;
}
 
void sieve_destroy(sieve* s) {
bit_array_destroy(&s->not_prime);
}
 
bool is_prime(const sieve* s, uint32_t n) {
assert(n <= s->limit);
return bit_array_get(&s->not_prime, n) == false;
}
 
uint32_t next_prime_digit_number(uint32_t n) {
if (n == 0)
return 2;
switch (n % 10) {
case 2:
return n + 1;
Line 264 ⟶ 399:
}
 
bool is_prime(integer n) {
int main() {
if (n < 2)
const uint32_t limit = 10000000;
sieve s = { 0return }false;
if (!sieve_create(&s,n limit))% {2 == 0)
fprintf(stderr,return "Outn of== memory\n")2;
if (n % 3 return== 1;0)
return n == 3;
if (n % 5 == 0)
return n == 5;
static const integer wheel[] = { 4,2,4,2,4,6,2,6 };
integer p = 7;
for (;;) {
for (int i = 0; i < 8; ++i) {
if (p * p > n)
return true;
if (n % p == 0)
return false;
p += wheel[i];
}
}
}
uint32_t n = 0, n1 = 0, n2 = 0, n3 = 0;
 
int main() {
setlocale(LC_ALL, "");
const integer limit = 1000000000;
integer n = 0, max = 0;
printf("First 25 SPDS primes:\n");
for (int i = 0; n < limit; ) {
n = next_prime_digit_number(n);
if (!is_prime(n >= limit))
breakcontinue;
if (is_prime(&s,i n)< 25) {
if (i <> 250) {
if printf(i >" 0");
printf("%'u", "n);
printf("%u", n);
}
else if (i == 25)
printf("\n");
++i;
if (i == 100)
n1 = n;
else if (i == 1000)
n2 = n;
n3 = n;
}
else if (i == 25)
printf("\n");
++i;
if (i == 100)
printf("Hundredth SPDS prime: %'u\n", n);
else if (i == 1000)
printf("Thousandth SPDS prime: %'u\n", n);
else if (i == 10000)
printf("Ten thousandth SPDS prime: %'u\n", n);
max = n;
}
printf("Largest SPDS prime less than %'u: %'u\n", limit, max);
sieve_destroy(&s);
printf("Hundredth SPDS prime: %u\n", n1);
printf("Thousandth SPDS prime: %u\n", n2);
printf("Largest SPDS prime less than %u: %u\n", limit, n3);
return 0;
}</langsyntaxhighlight>
 
{{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 2,237 22372, 2273273
Hundredth SPDS prime: 3322333,223
Thousandth SPDS prime: 32735273,273,527
LargestTen thousandth SPDS prime less than 10000000: 7777753273,322,727
Largest SPDS prime less than 1,000,000,000: 777,777,773
</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
#include "prime_sieve.hpp"
 
using integer = uint32_t;
Line 319 ⟶ 469:
if (n == 0)
return 2;
switch (n % 10) {
case 2:
return n + 1;
Line 327 ⟶ 477:
default:
return 2 + next_prime_digit_number(n/10) * 10;
}
}
 
bool is_prime(integer n) {
if (n < 2)
return false;
if (n % 2 == 0)
return n == 2;
if (n % 3 == 0)
return n == 3;
if (n % 5 == 0)
return n == 5;
constexpr integer wheel[] = { 4,2,4,2,4,6,2,6 };
integer p = 7;
for (;;) {
for (integer w : wheel) {
if (p * p > n)
return true;
if (n % p == 0)
return false;
p += w;
}
}
}
 
int main() {
std::cout.imbue(std::locale(""));
const integer limit = 10000000;
prime_sieveconst integer sieve(limit) = 1000000000;
integer n = 0, n1 = 0, n2 = 0, n3max = 0;
std::cout << "First 25 SPDS primes:\n";
for (int i = 0; n < limit; ) {
n = next_prime_digit_number(n);
if (!is_prime(n >= limit))
breakcontinue;
if (sieve.is_prime(n)i < 25) {
if (i <> 250) {
ifstd::cout (i<< >' 0)';
std::cout << ", "n;
std::cout << n;
}
else if (i == 25)
std::cout << '\n';
++i;
if (i == 100)
n1 = n;
else if (i == 1000)
n2 = n;
n3 = n;
}
else if (i == 25)
std::cout << '\n';
++i;
if (i == 100)
std::cout << "Hundredth SPDS prime: " << n << '\n';
else if (i == 1000)
std::cout << "Thousandth SPDS prime: " << n << '\n';
else if (i == 10000)
std::cout << "Ten thousandth SPDS prime: " << n << '\n';
max = n;
}
std::cout << "HundredthLargest SPDS prime less than " << limit << ": " << n1max << '\n';
std::cout << "Thousandth SPDS prime: " << n2 << '\n';
std::cout << "Largest SPDS prime less than " << limit << ": " << n3 << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Contents of prime_sieve.hpp:
<pre>
<lang cpp>#ifndef PRIME_SIEVE_HPP
First 25 SPDS primes:
#define PRIME_SIEVE_HPP
2 3 5 7 23 37 53 73 223 227 233 257 277 337 353 373 523 557 577 727 733 757 773 2,237 2,273
Hundredth SPDS prime: 33,223
Thousandth SPDS prime: 3,273,527
Ten thousandth SPDS prime: 273,322,727
Largest SPDS prime less than 1,000,000,000: 777,777,773
</pre>
 
=={{header|Delphi}}==
#include <algorithm>
{{works with|Delphi|6.0}}
#include <vector>
{{libheader|SysUtils,StdCtrls}}
Uses the [[Extensible_prime_generator#Delphi|Delphi Prime-Generator Object]]
 
<syntaxhighlight lang="Delphi">
/**
procedure ShowSmarandachePrimes(Memo: TMemo);
* A simple implementation of the Sieve of Eratosthenes.
{Show primes where all digits are also prime}
* See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.
var Sieve: TPrimeSieve;
*/
var I,J,P,Count: integer;
class prime_sieve {
var S: string;
public:
explicit prime_sieve(size_t);
bool is_prime(size_t) const;
private:
std::vector<bool> is_prime_;
};
 
/**
* Constructs a sieve with the given limit.
*
* @param limit the maximum integer that can be tested for primality
*/
inline prime_sieve::prime_sieve(size_t limit) {
limit = std::max(size_t(3), limit);
is_prime_.resize(limit/2, true);
for (size_t p = 3; p * p <= limit; p += 2) {
if (is_prime_[p/2 - 1]) {
size_t inc = 2 * p;
for (size_t q = p * p; q <= limit; q += inc)
is_prime_[q/2 - 1] = false;
}
}
}
 
function AllDigitsPrime(N: integer): boolean;
/**
{Test all digits on N to see if they are prime}
* Returns true if the given integer is a prime number. The integer
var I,Count: integer;
* must be less than or equal to the limit passed to the constructor.
var IA: TIntegerDynArray;
*
begin
* @param n an integer less than or equal to the limit passed to the
Result:=False;
* constructor
GetDigits(N,IA);
* @return true if the integer is prime
for I:=0 to High(IA) do
*/
if not Sieve.Flags[IA[I]] then exit;
inline bool prime_sieve::is_prime(size_t n) const {
Result:=True;
if (n == 2)
end;
return true;
if (n < 2 || n % 2 == 0)
return false;
return is_prime_.at(n/2 - 1);
}
 
#endif</lang>
 
begin
Sieve:=TPrimeSieve.Create;
try
{Build 1 million primes}
Sieve.Intialize(1000000);
Count:=0;
{Test if all digits of the number are prime}
for I:=0 to Sieve.PrimeCount-1 do
begin
P:=Sieve.Primes[I];
if AllDigitsPrime(P) then
begin
Inc(Count);
if Count<=25 then Memo.Lines.Add(IntToStr(Count)+' - '+IntToStr(P));
if Count=100 then
begin
Memo.Lines.Add('100th = '+IntToStr(P));
break;
end;
end;
end;
finally Sieve.Free; end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
1 - 2
First 25 SPDS primes:
2 - 3
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
3 - 5
Hundredth SPDS prime: 33223
4 - 7
Thousandth SPDS prime: 3273527
5 - 23
Largest SPDS prime less than 10000000: 7777753
6 - 37
7 - 53
8 - 73
9 - 223
10 - 227
11 - 233
12 - 257
13 - 277
14 - 337
15 - 353
16 - 373
17 - 523
18 - 557
19 - 577
20 - 727
21 - 733
22 - 757
23 - 773
24 - 2237
25 - 2273
100th = 33223
Elapsed Time: 150.037 ms.
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
n = 2
repeat
if isprim n = 1
h = n
while h > 0
d = h mod 10
if d < 2 or d = 4 or d = 6 or d > 7
break 1
.
h = h div 10
.
if h = 0
cnt += 1
if cnt <= 25
write n & " "
.
.
.
until cnt = 100
n += 1
.
print ""
print n
</syntaxhighlight>
 
{{out}}
<pre>
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
33223
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Generate Smarandache prime-digital sequence. Nigel Galloway: May 31st., 2019
let rec spds g=seq{yield! g; yield! (spds (Seq.collect(fun g->[g*10+2;g*10+3;g*10+5;g*10+7]) g))}|>Seq.filter(isPrime)
Line 432 ⟶ 676:
printfn "\n\n100th item of this sequence is %d" (spds [2;3;5;7] |> Seq.item 99)
printfn "1000th item of this sequence is %d" (spds [2;3;5;7] |> Seq.item 999)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 468 ⟶ 712:
=={{header|Factor}}==
===Naive===
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit io lists lists.lazy math
math.parser math.primes prettyprint sequences ;
IN: rosetta-code.smarandache-naive
Line 485 ⟶ 729:
"100th member: " write smarandache 99 [ cdr ] times car . ;
 
MAIN: smarandache-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 520 ⟶ 764:
 
===Optimized===
<langsyntaxhighlight lang="factor">USING: combinators generalizations io kernel math math.functions
math.primes prettyprint sequences ;
IN: rosetta-code.smarandache
Line 574 ⟶ 818:
] each ;
 
MAIN: smarandache-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 609 ⟶ 853:
100000th member: 23325232253
</pre>
 
=={{header|Forth}}==
<syntaxhighlight 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
5 = if 2 + exit then
10 / recurse 10 * 2 + ;
 
: spds_next ( n -- n )
begin
next_prime_digit_number
dup is_prime?
until ;
 
: spds_print ( n -- )
0 swap 0 do
spds_next dup .
loop
drop cr ;
 
: spds_nth ( n -- n )
0 swap 0 do spds_next loop ;
 
." First 25 SPDS primes:" cr
25 spds_print
 
." 100th SPDS prime: "
100 spds_nth . cr
 
." 1000th SPDS prime: "
1000 spds_nth . cr
 
bye</syntaxhighlight>
 
{{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>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
function isprime( n as ulongint ) as boolean
if n < 2 then return false
if n = 2 then return true
if n mod 2 = 0 then return false
for i as uinteger = 3 to int(sqr(n))+1 step 2
if n mod i = 0 then return false
next i
return true
end function
 
dim as integer smar(1 to 100), count = 1, i = 1, digit, j
smar(1) = 2
print 1, 2
while count < 100
i += 2
if not isprime(i) then continue while
for j = 1 to len(str(i))
digit = val(mid(str(i),j,1))
if not isprime(digit) then continue while
next j
count += 1
smar(count) = i
if count = 100 orelse count <=25 then
print count, smar(count)
end if
wend</syntaxhighlight>
{{out}}
<pre>
1 2
2 3
3 5
4 7
5 23
6 37
7 53
8 73
9 223
10 227
11 233
12 257
13 277
14 337
15 353
16 373
17 523
18 557
19 577
20 727
21 733
22 757
23 773
24 2237
25 2273
100 33223</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Smarandache_prime-digital_sequence}}
 
'''Solution'''
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 01.png]]
 
'''Case 1. Show the first 25 SPDS primes'''
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 02.png]]
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 03.png]]
 
'''Case 2. Show the hundredth SPDS prime'''
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 04.png]]
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 05.png]]
 
'''Additional cases. Show the 1000-th, 10,000-th and 100,000th SPDS primes'''
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 06.png]]
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 07.png]]
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 08.png]]
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 09.png]]
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 10.png]]
 
[[File:Fōrmulæ - Smarandache prime-digital sequence 11.png]]
 
=={{header|Go}}==
===Basic===
<langsyntaxhighlight lang="go">package main
 
import (
Line 665 ⟶ 1,058:
n = listSPDSPrimes(n+2, indices[i-1], indices[i], true)
}
}</langsyntaxhighlight>
 
{{out}}
Line 712 ⟶ 1,105:
 
This is more than 30 times faster than the above version (runs in about 12.5 seconds on my Celeron @1.6GHx) and could be quickened up further (to around 4 seconds) by using a wrapper for GMP rather than Go's native big.Int type.
<langsyntaxhighlight lang="go">package main
 
import (
Line 799 ⟶ 1,192:
n = listSPDSPrimes(n.AddTwo(), indices[i-1], indices[i], true)
}
}</langsyntaxhighlight>
 
{{out}}
Line 805 ⟶ 1,198:
Same as before.
</pre>
 
=={{header|Haskell}}==
Using the optimized approach of generated numbers from prime digits and testing for primality.
<langsyntaxhighlight lang="haskell">{-# LANGUAGE NumericUnderscores #-}
import Control.Monad (guard)
import Math.NumberTheory.Primes.Testing (isPrime)
Line 835 ⟶ 1,229:
mapM_ (uncurry (printf "The %9sth SPDS: %15s\n")) $
nextSPDSTerms [100, 1_000, 10_000, 100_000, 1_000_000]
where f = show . take 25</langsyntaxhighlight>
{{out}}
<pre>The first 25 SPDS:
Line 871 ⟶ 1,265:
=={{header|Java}}==
Generate next in sequence directly from previous, inspired by previous solutions.
<langsyntaxhighlight lang="java">
public class SmarandachePrimeDigitalSequence {
 
Line 954 ⟶ 1,348:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 964 ⟶ 1,358:
10,000th Smarandache prime-digital sequence number = 273322727
100,000th Smarandache prime-digital sequence number = 23325232253
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
See the preamble to the [[#Julia|Julia]] entry for the rationale behind the following implementation.
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime` as used here.
<syntaxhighlight lang="jq">def Smarandache_primes:
# Output: a naively constructed stream of candidate strings of length >= 1
def Smarandache_candidates:
def unconstrained($length):
if $length==1 then "2", "3", "5", "7"
else ("2", "3", "5", "7") as $n
| $n + unconstrained($length -1 )
end;
unconstrained(. - 1) as $u
| ("3", "7") as $tail
| $u + $tail ;
 
2,3,5,7,
(range(2; infinite) | Smarandache_candidates | tonumber | select(is_prime));
 
# Override jq's incorrect definition of nth/2
# Emit the $n-th value of the stream, counting from 0; or emit nothing
def nth($n; s):
if $n < 0 then error("nth/2 doesn't support negative indices")
else label $out
| foreach s as $x (-1; .+1; select(. >= $n) | $x, break $out)
end;
 
"First 25:",
[limit(25; Smarandache_primes)],
 
# jq counts from 0 so:
"\nThe hundredth: \(nth(99; Smarandache_primes))"</syntaxhighlight>
{{out}}
<pre>
jq -nrc -f rc-smarandache-primes.jq
First 25:
[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]
 
The hundredth: 33223
</pre>
 
Line 971 ⟶ 1,409:
add numbers that end in 3 or 7 and that only contain 2, 3, 5, and 7. This
can be done via permutations of combinations with repetition.
<langsyntaxhighlight lang="julia">
using Combinatorics, Primes
 
Line 997 ⟶ 1,435:
println("The 100th Smarandache prime is: ", v[100])
println("The 10000th Smarandache prime is: ", v[10000])
</langsyntaxhighlight>{{out}}
<pre>
The first 25 Smarandache primes are: [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]
Line 1,005 ⟶ 1,443:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- FUNCS:
local function T(t) return setmetatable(t, {__index=table}) end
table.firstn = function(t,n) local s=T{} n=n>#t and #t or n for i = 1,n do s[i]=t[i] end return s end
Line 1,022 ⟶ 1,460:
end
print("1-25 : " .. spds:firstn(25):concat(" "))
print("100th: " .. spds[100])</langsyntaxhighlight>
{{out}}
<pre>1-25 : 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: 33223</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[SmarandachePrimeQ]
SmarandachePrimeQ[n_Integer] := MatchQ[IntegerDigits[n], {(2 | 3 | 5 | 7) ..}] \[And] PrimeQ[n]
s = Select[Range[10^5], SmarandachePrimeQ];
Take[s, UpTo[25]]
s[[100]]</syntaxhighlight>
{{out}}
<pre>{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}
33223</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import math, strformat, strutils
 
const N = 35_000
 
# Sieve.
var composite: array[0..N, bool] # Default is false and means prime.
composite[0] = true
composite[1] = true
for n in 2..sqrt(N.toFloat).int:
if not composite[n]:
for k in countup(n * n, N, n):
composite[k] = true
 
 
func digits(n: Positive): seq[0..9] =
var n = n.int
while n != 0:
result.add n mod 10
n = n div 10
 
 
proc isSPDS(n: int): bool =
if composite[n]: return false
result = true
for d in n.digits:
if composite[d]: return false
 
 
iterator spds(maxCount: Positive): int {.closure.} =
yield 2
var count = 1
var n = 3
while count != maxCount and n <= N:
if n.isSPDS:
inc count
yield n
inc n, 2
if count != maxCount:
quit &"Too few values ({count}). Please, increase value of N.", QuitFailure
 
 
stdout.write "The first 25 SPDS are:"
for n in spds(25):
stdout.write ' ', n
echo()
 
var count = 0
for n in spds(100):
inc count
if count == 100:
echo "The 100th SPDS is: ", n</syntaxhighlight>
 
{{out}}
<pre>The first 25 SPDS are: 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
The 100th SPDS is: 33223</pre>
 
=={{header|Pascal}}==
Line 1,031 ⟶ 1,536:
uses [[http://rosettacode.org/wiki/Extensible_prime_generator#Pascal:Extensible_prime_generator]]<BR>
Simple Brute force.Testing for prime takes most of the time.
<langsyntaxhighlight lang="pascal">program Smarandache;
 
uses
Line 1,102 ⟶ 1,607:
inc(DgtLimit);
until DgtLimit= 12;
end.</langsyntaxhighlight>
{{out}}
<pre>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
Line 1,112 ⟶ 1,617:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,134 ⟶ 1,639:
 
say 'Smarandache prime-digitals:';
printf "%22s: %s\n", ucfirst(num2en_ordinal($_)), $spds[$_-1] for 1..25, 100, 1000, 10_000, 100_000;</langsyntaxhighlight>
{{out}}
<pre> First: 2
Line 1,171 ⟶ 1,676:
if they ended in 2 or 5), which we efficiently achieve by alternately adding {4,-4}. Digits to the left
of that must all be 2/3/5/7, so we add {1,2,2,-5}*10^k to cycle round those digits.
Otherwise it is exactly like counting by adding 1 to each digit and carrying 1 left when we do a 9->0.,
or in this case 7->2|3.
 
I had planned to effectively merge a list of potential candidates with a list of all prime numbers,
but because of the massive gaps (eg between 777,777,777 and 2,222,222,223) it proved much faster
to test each candidate for primality individually. Timings below show just how much this improves things.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>atom t0 = time()
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence spds = {2,3,5,7}
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
atom nxt_candidate = 23
<span style="color: #004080;">sequence</span> <span style="color: #000000;">spds</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}</span>
sequence adj = {{4,-4},sq_mul({1,2,2,-5},10)},
<span style="color: #004080;">atom</span> <span style="color: #000000;">nxt_candidate</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">23</span>
adjn = {1,1}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)},</span>
 
<span style="color: #000000;">adjn</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
include mpfr.e
mpz zprime = mpz_init()
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
randstate state = gmp_randinit_mt()
<span style="color: #004080;">mpz</span> <span style="color: #000000;">zprime</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
 
procedure populate_spds(integer n)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">populate_spds</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
while length(spds)<n do
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">spds</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
mpz_set_d(zprime,nxt_candidate)
<span style="color: #7060A8;">mpz_set_d</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zprime</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nxt_candidate</span><span style="color: #0000FF;">)</span>
if mpz_probable_prime_p(zprime,state) then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zprime</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
spds &= nxt_candidate
<span style="color: #000000;">spds</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">nxt_candidate</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for i=1 to length(adjn) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">adjn</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
sequence adjs = adj[i]
<span style="color: #004080;">sequence</span> <span style="color: #000000;">adjs</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">adj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
integer adx = adjn[i]
<span style="color: #004080;">integer</span> <span style="color: #000000;">adx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">adjn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
nxt_candidate += adjs[adx]
<span style="color: #000000;">nxt_candidate</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">adjs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">adx</span><span style="color: #0000FF;">]</span>
adx += 1
<span style="color: #000000;">adx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if adx<=length(adjs) then
<span style="color: #008080;">if</span> <span style="color: #000000;">adx</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">adjs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
adjn[i] = adx
<span style="color: #000000;">adjn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">adx</span>
exit
end if <span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
adjn[i] = 1
<span style="color: #000000;">adjn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
if i=length(adjn) then
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">adjn</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
-- (this is eg 777, by now 223 carry 1, -> 2223)
<span style="color: #000080;font-style:italic;">-- (this is eg 777, by now 223 carry 1, -&gt; 2223)</span>
adj = append(adj,sq_mul(adj[$],10))
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">adj</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">adj</span><span style="color: #0000FF;">[$],</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
adjn = append(adjn, 1)
<span style="color: #000000;">adjn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">adjn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
nxt_candidate += adj[$][2]
<span style="color: #000000;">nxt_candidate</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">adj</span><span style="color: #0000FF;">[$][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
exit
end if <span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
 
populate_spds(25)
<span style="color: #000000;">populate_spds</span><span style="color: #0000FF;">(</span><span style="color: #000000;">25</span><span style="color: #0000FF;">)</span>
printf(1,"spds[1..25]:%v\n",{spds[1..25]})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"spds[1..25]:%v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">spds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">25</span><span style="color: #0000FF;">]})</span>
for n=2 to 5 do
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
integer p = power(10,n)
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
populate_spds(p)
<span style="color: #000000;">populate_spds</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
printf(1,"spds[%d]:%d\n",{p,spds[p]})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"spds[%,d]:%,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">spds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]})</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for n=7 to 10 do
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">7</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
atom p = power(10,n),
<span style="color: #004080;">atom</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span>
dx = abs(binary_search(p,spds))-1
<span style="color: #000000;">dx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">binary_search</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">spds</span><span style="color: #0000FF;">))-</span><span style="color: #000000;">1</span>
printf(1,"largest spds prime less than %,15d:%,14d\n",{p,spds[dx]})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"largest spds prime less than %,15d:%,14d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">spds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">]})</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
?elapsed(time()-t0)</lang>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,237 ⟶ 1,745:
largest spds prime less than 1,000,000,000: 777,777,773
largest spds prime less than 10,000,000,000: 7,777,777,577
"34.6s"
</pre>
For comparison, on the same machine:<br>
Line 1,246 ⟶ 1,754:
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
def divisors(n):
divs = [1]
Line 1,292 ⟶ 1,800:
pass
print(100, generator.__next__())
</syntaxhighlight>
</lang>
 
<b>Output</b>
<syntaxhighlight lang="python">
<lang Python>
1 2
2 3
Line 1,312 ⟶ 1,820:
15 353
100 33223
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
===Naive===
 
<syntaxhighlight lang="quackery"> [ true swap
[ 10 /mod
[ table 1 1 0 0 1 0 1 0 1 1 ]
iff [ dip not ] done
dup 0 = until ]
drop ] is digitsprime ( n --> b )
 
[ temp put [] 0
[ dup digitsprime if
[ dup isprime if
[ dup dip join ] ]
1+
over size temp share = until ]
drop ] is spds ( n --> [ )
 
100 spds
25 split swap echo
cr cr
-1 peek echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 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 ]
 
33223</pre>
 
===Optimised===
 
Not the same as the Factor and Factor inspired solutions, which count in base 4 with leading zeros like a telescoping pedometer; this skips over base 5 numbers with zeros in them.
 
<syntaxhighlight lang="quackery"> [ 0 over
[ 5 /mod 0 = while
dip [ 5 * 1+ ]
again ]
drop + ] is skipzeros ( n --> n )
 
[ [] swap
[ 5 /mod
[ table 0 2 3 5 7 ]
rot join swap
dup 0 = until ]
swap witheach
[ swap 10 * + ] ] is primedigits ( n --> n )
 
 
[ temp put [] 0
[ 1+ skipzeros
dup primedigits
dup isprime iff
[ swap dip join ]
else drop
over size
temp share = until ]
temp release drop ] is spds ( n --> [ )
 
100 spds
25 split swap echo
cr cr
-1 peek echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 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 ]
 
33223</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{libheader|ntheory}}
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
use ntheory:from<Perl5> <:all>;
 
# Implemented as a lazy, extendable list
my $spds = grep { .&is_prime }, flat [2,3,5,7], [23,27,33,37,53,57,73,77], -> $p
{ state $o++; my $oom = 10**(1+$o); [ flat (2,3,5,7).map: -> $l { (|$p).map: $l*$oom + * } ] } … *;
 
say 'Smarandache prime-digitals:';
printf "%22s: %s\n", ordinal(1+$_).tclc, comma $spds[$_] for flat ^25, 99, 999, 9999, 99999;</langsyntaxhighlight>
{{out}}
<pre>Smarandache prime-digitals:
Line 1,360 ⟶ 1,940:
=={{header|REXX}}==
The prime number generator has been simplified and very little optimization was included.
<langsyntaxhighlight lang="rexx">/*REXX program lists a sequence of SPDS (Smarandache prime-digital sequence) primes.*/
parse arg n q /*get optional number of primes to find*/
if n=='' | n=="," then n= 25 /*Not specified? Then use the default.*/
Line 1,389 ⟶ 1,969:
end /*j*/ /* [↑] only display N number of primes*/
if ox<0 then say right(z, 21) /*display one (the last) SPDS prime. */
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,427 ⟶ 2,007:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project: Calmo primes
load "stdlib.ring"
 
limit = 25
see "First 25 Smarandache primes:" + nl + nl
max = 300000
 
num = 0
limit = 26
see "working..." + nl
limit100 = 100
see "wait for done..." + nl
for n = 1 to 34000
see "First 25 Calmo primes are:" + nl
for n = 1 toflag = max0
ifnStr isprime= string(n)
for x = res1 =to calmolen(nnStr)
if resnx = 1number(nStr[x])
if isprime(n) numand = num + 1isprime(nx)
if numflag <= limitflag + 1
else
see "" + num + ". " + n + nl
ok exit
if num = 100ok
next
see "The hundredth Calmo prime is:" + nl
if flag = len(nStr)
see "" + num + ". " + n + nl
num = num + exit1
if num ok< limit
ok see "" + n + " "
ok
if num = limit100
see nl + nl + "100th Smarandache prime: " + n + nl
ok
ok
next
</syntaxhighlight>
see "done..." + nl
 
func calmo(p)
sp = string(p)
for n = 1 to len(sp)
if not isprime(sp[n])
return 0
ok
next
return 1
</lang>
{{Out}}
<pre>
First 25 Smarandache primes:
working...
 
wait for done...
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
First 25 Calmo primes are:
 
1. 2
100th Smarandache prime: 33223
2. 3
</pre>
3. 5
 
4. 7
=={{header|RPL}}==
5. 23
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.
6. 37
<code>PRIM?</code> is defined at [[Primality by trial division#RPL|Primality by trial division]].
7. 53
{| class="wikitable"
8. 73
! RPL code
9. 223
! Comment
10. 227
|-
11. 233
|
12. 257
≪ { "2" "3" "5" "7" } DUP SIZE → digits base
13. 277
≪ DUP SIZE 1 CF 2 SF
14. 337
'''DO'''
15. 353
'''IF''' DUP NOT '''THEN''' digits 1 GET ROT + SWAP 1 CF
16. 373
'''ELSE'''
17. 523
DUP2 DUP SUB digits SWAP POS
18. 557
'''IF''' 2 FS?C '''THEN''' 2 == 4 ≪ 1 SF 2 ≫ IFTE
19. 577
'''ELSE IF''' DUP base == '''THEN'''
20. 727
SIGN 1 SF '''ELSE''' 1 + 1 CF '''END'''
21. 733
'''END'''
22. 757
digits SWAP GET REPL
23. 773
LASTARG ROT DROP2 1 - '''END'''
24. 2237
'''UNTIL''' 1 FC? '''END''' DROP
25. 2273
≫ ≫ ‘<span style="color:blue">'''NSPDP'''</span>’ STO
The hundredth Calmo prime is:
|
100. 33223
<span style="color:blue">'''NSPDP'''</span> ''( "PDnumber" → "nextPDnumber" )''
done...
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}}==
<syntaxhighlight lang="rust">fn is_prime(n: u32) -> bool {
if n < 2 {
return false;
}
if n % 2 == 0 {
return n == 2;
}
if n % 3 == 0 {
return n == 3;
}
if n % 5 == 0 {
return n == 5;
}
let mut p = 7;
const WHEEL: [u32; 8] = [4, 2, 4, 2, 4, 6, 2, 6];
loop {
for w in &WHEEL {
if p * p > n {
return true;
}
if n % p == 0 {
return false;
}
p += w;
}
}
}
 
fn next_prime_digit_number(n: u32) -> u32 {
if n == 0 {
return 2;
}
match n % 10 {
2 => n + 1,
3 | 5 => n + 2,
_ => 2 + next_prime_digit_number(n / 10) * 10,
}
}
 
fn smarandache_prime_digital_sequence() -> impl std::iter::Iterator<Item = u32> {
let mut n = 0;
std::iter::from_fn(move || {
loop {
n = next_prime_digit_number(n);
if is_prime(n) {
break;
}
}
Some(n)
})
}
 
fn main() {
let limit = 1000000000;
let mut seq = smarandache_prime_digital_sequence().take_while(|x| *x < limit);
println!("First 25 SPDS primes:");
for i in seq.by_ref().take(25) {
print!("{} ", i);
}
println!();
if let Some(p) = seq.by_ref().nth(99 - 25) {
println!("100th SPDS prime: {}", p);
}
if let Some(p) = seq.by_ref().nth(999 - 100) {
println!("1000th SPDS prime: {}", p);
}
if let Some(p) = seq.by_ref().nth(9999 - 1000) {
println!("10,000th SPDS prime: {}", p);
}
if let Some(p) = seq.last() {
println!("Largest SPDS prime less than {}: {}", limit, p);
}
}</syntaxhighlight>
 
{{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
10,000th SPDS prime: 273322727
Largest SPDS prime less than 1000000000: 777777773
</pre>
 
=={{header|Ruby}}==
Attaching 3 and 7 to permutations of 2,3,5 and 7
<langsyntaxhighlight lang="ruby">require "prime"
smarandache = Enumerator.new do|y|
Line 1,517 ⟶ 2,200:
p seq.first(25)
p seq.last
</syntaxhighlight>
</lang>
{{out}}
<pre>[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]
Line 1,525 ⟶ 2,208:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_prime_digital(n) {
n.is_prime && n.digits.all { .is_prime }
}
 
say is_prime_digital.first(25).join(',')
say is_prime_digital.nth(100)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,536 ⟶ 2,219:
33223
</pre>
 
=={{header|Swift}}==
{{trans|C++}}
<syntaxhighlight lang="swift">func isPrime(number: Int) -> Bool {
if number < 2 {
return false
}
if number % 2 == 0 {
return number == 2
}
if number % 3 == 0 {
return number == 3
}
if number % 5 == 0 {
return number == 5
}
var p = 7
let wheel = [4,2,4,2,4,6,2,6]
while true {
for w in wheel {
if p * p > number {
return true
}
if number % p == 0 {
return false
}
p += w
}
}
}
 
func nextPrimeDigitNumber(number: Int) -> Int {
if number == 0 {
return 2
}
switch number % 10 {
case 2:
return number + 1
case 3, 5:
return number + 2
default:
return 2 + nextPrimeDigitNumber(number: number/10) * 10
}
}
 
let limit = 1000000000
var n = 0
var max = 0
var count = 0
print("First 25 SPDS primes:")
while n < limit {
n = nextPrimeDigitNumber(number: n)
if !isPrime(number: n) {
continue
}
if count < 25 {
print(n, terminator: " ")
} else if count == 25 {
print()
}
count += 1
if (count == 100) {
print("Hundredth SPDS prime: \(n)")
} else if (count == 1000) {
print("Thousandth SPDS prime: \(n)")
} else if (count == 10000) {
print("Ten thousandth SPDS prime: \(n)")
}
max = n
}
print("Largest SPDS prime less than \(limit): \(max)")</syntaxhighlight>
 
{{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
Hundredth SPDS prime: 33223
Thousandth SPDS prime: 3273527
Ten thousandth SPDS prime: 273322727
Largest SPDS prime less than 1000000000: 777777773
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
Simple brute-force approach.
<syntaxhighlight lang="wren">import "./math" for Int
 
var limit = 1000
var spds = List.filled(limit, 0)
spds[0] = 2
var i = 3
var count = 1
while (count < limit) {
if (Int.isPrime(i)) {
var digits = i.toString
if (digits.all { |d| "2357".contains(d) }) {
spds[count] = i
count = count + 1
}
}
i = i + 2
if (i > 10) {
var j = i % 10
if (j == 1 || j == 5) {
i = i + 2
} else if (j == 9) {
i = i + 4
}
}
}
System.print("The first 25 SPDS primes are:")
System.print(spds.take(25).toList)
System.print("\nThe 100th SPDS prime is %(spds[99])")
System.print("\nThe 1,000th SPDS prime is %(spds[999])")</syntaxhighlight>
 
{{out}}
<pre>
The first 25 SPDS primes are:
[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]
 
The 100th SPDS prime is 33223
 
The 1,000th SPDS prime is 3273527
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
 
func PrimeDigits(N); \Return 'true' if all digits are prime
int N;
[repeat N:= N/10;
case rem(0) of
0, 1, 4, 6, 8, 9: return false
other [];
until N = 0;
return true;
];
 
int C, N;
[C:= 0; N:= 2;
loop [if IsPrime(N) then
if PrimeDigits(N) then
[C:= C+1;
if C <= 25 then
[IntOut(0, N); ChOut(0, ^ )];
if C = 100 then
[Text(0, "^m^j100th: "); IntOut(0, N)];
if C = 1000 then quit;
];
N:= N+1;
];
Text(0, "^m^j1000th: "); IntOut(0, N); CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
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: 33223
1000th: 3273527
</pre>
 
=={{header|Yabasic}}==
{{trans|Ring}}
<syntaxhighlight lang="yabasic">num = 0
limit = 26
limit100 = 100
 
print "First 25 Smarandache primes:\n"
for n = 1 to 34000
flag = 0
nStr$ = str$(n)
for x = 1 to len(nStr$)
nx = val(mid$(nStr$,x,1))
if isPrime(n) and isPrime(nx) then
flag = flag + 1
else
break
end if
next
if flag = len(nStr$) then
num = num + 1
if num < limit print "", n, " ";
if num = limit100 print "\n\n100th Smarandache prime: ", n
end if
next n
end
 
sub isPrime(v)
if v < 2 return False
if mod(v, 2) = 0 return v = 2
if mod(v, 3) = 0 return v = 3
d = 5
while d * d <= v
if mod(v, d) = 0 then return False else d = d + 2 : fi
wend
return True
end sub</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de Ring.</pre>
 
 
=={{header|zkl}}==
Line 1,543 ⟶ 2,435:
 
[[Extensible prime generator#zkl]] could be used instead.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
 
spds:=Walker.zero().tweak(fcn(ps){
Line 1,550 ⟶ 2,442:
if(p.split().filter( fcn(n){ 0==nps[n] }) ) return(Void.Skip);
p // 733 --> (7,3,3) --> () --> good, 29 --> (2,9) --> (9) --> bad
}.fp(BI(1)));</langsyntaxhighlight>
Or
<langsyntaxhighlight lang="zkl">spds:=Walker.zero().tweak(fcn(ps){
var [const] nps="014689".inCommon;
p:=ps.nextPrime().toInt();
if(nps(p.toString())) return(Void.Skip);
p // 733 --> "" --> good, 29 --> "9" --> bad
}.fp(BI(1)));</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("The first 25 terms of the Smarandache prime-digital sequence are:");
spds.walk(25).concat(",").println();
 
println("The hundredth term of the sequence is: ",spds.drop(100-25).value);
println("1000th item of this sequence is : ",spds.drop(1_000-spds.n).value);</langsyntaxhighlight>
{{out}}
<pre>
1,978

edits