Smarandache prime-digital sequence: Difference between revisions

Added Easylang
(Added 11l)
(Added Easylang)
 
(25 intermediate revisions by 16 users not shown)
Line 19:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F divisors(n)
V divs = [1]
L(ii) 2 .< Int(n ^ 0.5) + 3
Line 59:
print(item, end' ‘ ’)
print()
print(‘Hundredth SPDS prime: ’seq[99])</langsyntaxhighlight>
 
{{out}}
Line 66:
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>
 
Line 71 ⟶ 163:
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 172 ⟶ 264:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 181 ⟶ 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 222 ⟶ 332:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 228 ⟶ 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 <locale.h>
#include <stdbool.h>
#include <stdint.h>
Line 301 ⟶ 448:
printf("Largest SPDS prime less than %'u: %'u\n", limit, max);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 314 ⟶ 461:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
 
Line 382 ⟶ 529:
std::cout << "Largest SPDS prime less than " << limit << ": " << max << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 392 ⟶ 539:
Ten thousandth SPDS prime: 273,322,727
Largest SPDS prime less than 1,000,000,000: 777,777,773
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses the [[Extensible_prime_generator#Delphi|Delphi Prime-Generator Object]]
 
<syntaxhighlight lang="Delphi">
procedure ShowSmarandachePrimes(Memo: TMemo);
{Show primes where all digits are also prime}
var Sieve: TPrimeSieve;
var I,J,P,Count: integer;
var S: string;
 
 
function AllDigitsPrime(N: integer): boolean;
{Test all digits on N to see if they are prime}
var I,Count: integer;
var IA: TIntegerDynArray;
begin
Result:=False;
GetDigits(N,IA);
for I:=0 to High(IA) do
if not Sieve.Flags[IA[I]] then exit;
Result:=True;
end;
 
 
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
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
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 402 ⟶ 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 438 ⟶ 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 455 ⟶ 729:
"100th member: " write smarandache 99 [ cdr ] times car . ;
 
MAIN: smarandache-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 490 ⟶ 764:
 
===Optimized===
<langsyntaxhighlight lang="factor">USING: combinators generalizations io kernel math math.functions
math.primes prettyprint sequences ;
IN: rosetta-code.smarandache
Line 544 ⟶ 818:
] each ;
 
MAIN: smarandache-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 581 ⟶ 855:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: is_prime? ( n -- flag )
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
Line 612 ⟶ 886:
: spds_print ( n -- )
0 swap 0 do
spds_next dup .
dup is_prime? if dup . then
loop
drop cr ;
Line 629 ⟶ 902:
1000 spds_nth . cr
 
bye</langsyntaxhighlight>
 
{{out}}
Line 640 ⟶ 913:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
function isprime( n as ulongint ) as boolean
if n < 2 then return false
Line 666 ⟶ 939:
print count, smar(count)
end if
wend</langsyntaxhighlight>
{{out}}
<pre>
Line 695 ⟶ 968:
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 751 ⟶ 1,058:
n = listSPDSPrimes(n+2, indices[i-1], indices[i], true)
}
}</langsyntaxhighlight>
 
{{out}}
Line 798 ⟶ 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 885 ⟶ 1,192:
n = listSPDSPrimes(n.AddTwo(), indices[i-1], indices[i], true)
}
}</langsyntaxhighlight>
 
{{out}}
Line 894 ⟶ 1,201:
=={{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 922 ⟶ 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 958 ⟶ 1,265:
=={{header|Java}}==
Generate next in sequence directly from previous, inspired by previous solutions.
<langsyntaxhighlight lang="java">
public class SmarandachePrimeDigitalSequence {
 
Line 1,041 ⟶ 1,348:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,051 ⟶ 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 1,058 ⟶ 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 1,084 ⟶ 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,092 ⟶ 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,109 ⟶ 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,118 ⟶ 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,189 ⟶ 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,199 ⟶ 1,617:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,221 ⟶ 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,258 ⟶ 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,324 ⟶ 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,333 ⟶ 1,754:
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
def divisors(n):
divs = [1]
Line 1,379 ⟶ 1,800:
pass
print(100, generator.__next__())
</syntaxhighlight>
</lang>
 
<b>Output</b>
<syntaxhighlight lang="python">
<lang Python>
1 2
2 3
Line 1,399 ⟶ 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,447 ⟶ 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,476 ⟶ 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,514 ⟶ 2,007:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,543 ⟶ 2,036:
ok
next
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,553 ⟶ 2,046:
</pre>
 
=={{header|RPL}}==
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.
<code>PRIM?</code> is defined at [[Primality by trial division#RPL|Primality by trial division]].
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ { "2" "3" "5" "7" } DUP SIZE → digits base
≪ DUP SIZE 1 CF 2 SF
'''DO'''
'''IF''' DUP NOT '''THEN''' digits 1 GET ROT + SWAP 1 CF
'''ELSE'''
DUP2 DUP SUB digits SWAP POS
'''IF''' 2 FS?C '''THEN''' 2 == 4 ≪ 1 SF 2 ≫ IFTE
'''ELSE IF''' DUP base == '''THEN'''
SIGN 1 SF '''ELSE''' 1 + 1 CF '''END'''
'''END'''
digits SWAP GET REPL
LASTARG ROT DROP2 1 - '''END'''
'''UNTIL''' 1 FC? '''END''' DROP
≫ ≫ ‘<span style="color:blue">'''NSPDP'''</span>’ STO
|
<span style="color:blue">'''NSPDP'''</span> ''( "PDnumber" → "nextPDnumber" )''
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}}==
<langsyntaxhighlight lang="rust">fn is_prime(n: u32) -> bool {
if n < 2 {
return false;
Line 1,626 ⟶ 2,169:
println!("Largest SPDS prime less than {}: {}", limit, p);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,640 ⟶ 2,183:
=={{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,657 ⟶ 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,665 ⟶ 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,679 ⟶ 2,222:
=={{header|Swift}}==
{{trans|C++}}
<langsyntaxhighlight lang="swift">func isPrime(number: Int) -> Bool {
if number < 2 {
return false
Line 1,746 ⟶ 2,289:
max = n
}
print("Largest SPDS prime less than \(limit): \(max)")</langsyntaxhighlight>
 
{{out}}
Line 1,761 ⟶ 2,304:
{{libheader|Wren-math}}
Simple brute-force approach.
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
var limit = 1000
Line 1,789 ⟶ 2,332:
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])")</langsyntaxhighlight>
 
{{out}}
Line 1,800 ⟶ 2,343:
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,807 ⟶ 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,814 ⟶ 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