Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/Smarandache_prime-digital_sequence
note: Prime Numbers

View file

@ -0,0 +1,15 @@
The Smarandache prime-digital sequence (SPDS for brevity) is the sequence of primes whose digits are themselves prime.
For example 257 is an element of this sequence because it is prime itself and its digits: 2, 5 and 7 are also prime.
;Task
* Show the first 25 SPDS primes.
* Show the hundredth SPDS prime.
;See also:
* [[oeis:A019546|OEIS A019546: Primes whose digits are primes.]]
* https://www.scribd.com/document/214851583/On-the-Smarandache-prime-digital-subsequence-sequences
<br><br>

View file

@ -0,0 +1,41 @@
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])

View file

@ -0,0 +1,102 @@
# 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 #
# primes must be 3 or 7 and the only prime digits are 2, 3, 5 and 7 #
# Needs --heap 256m for Algol 68G #
BEGIN
# construct a sieve of primes up to 10 000 000 #
INT prime max = 10 000 000;
[ prime max ]BOOL prime; FOR i TO UPB prime DO prime[ i ] := TRUE OD;
FOR s FROM 2 TO ENTIER sqrt( prime max ) DO
IF prime[ s ] THEN
FOR p FROM s * s BY s TO prime max DO prime[ p ] := FALSE OD
FI
OD;
# consruct the Smarandache primes up to 10 000 000 #
[ prime max ]BOOL smarandache; FOR i TO UPB prime DO smarandache[ i ] := FALSE OD;
[ ]INT prime digits = ( 2, 3, 5, 7 );
[ 7 ]INT digits := ( 0, 0, 0, 0, 0, 0, 0 );
# tests whether the current digits form a Smarandache prime #
PROC try smarandache = VOID:
BEGIN
INT possible prime := 0;
FOR i TO UPB digits DO
possible prime *:= 10 +:= digits[ i ]
OD;
smarandache[ possible prime ] := prime[ possible prime ]
END # try smarandache # ;
# tests whether the current digits plus 3 or 7 form a Smarandache prime #
PROC try smarandache 3 or 7 = VOID:
BEGIN
digits[ UPB digits ] := 3;
try smarandache;
digits[ UPB digits ] := 7;
try smarandache
END # try smarandache 3 or 7 # ;
# the 1 digit primes are all Smarandache primes #
FOR d7 TO UPB prime digits DO smarandache[ prime digits[ d7 ] ] := TRUE OD;
# try the possible 2, 3, etc. digit numbers composed of prime digits #
FOR d6 TO UPB prime digits DO
digits[ 6 ] := prime digits[ d6 ];
try smarandache 3 or 7;
FOR d5 TO UPB prime digits DO
digits[ 5 ] := prime digits[ d5 ];
try smarandache 3 or 7;
FOR d4 TO UPB prime digits DO
digits[ 4 ] := prime digits[ d4 ];
try smarandache 3 or 7;
FOR d3 TO UPB prime digits DO
digits[ 3 ] := prime digits[ d3 ];
try smarandache 3 or 7;
FOR d2 TO UPB prime digits DO
digits[ 2 ] := prime digits[ d2 ];
try smarandache 3 or 7;
FOR d1 TO UPB prime digits DO
digits[ 1 ] := prime digits[ d1 ];
try smarandache 3 or 7
OD;
digits[ 1 ] := 0
OD;
digits[ 2 ] := 0
OD;
digits[ 3 ] := 0
OD;
digits[ 4 ] := 0
OD;
digits[ 5 ] := 0
OD;
# print some Smarandache primes #
INT count := 0;
INT s100 := 0;
INT s1000 := 0;
INT s last := 0;
INT p last := 0;
print( ( "First 25 Smarandache primes:", newline ) );
FOR i TO UPB smarandache DO
IF smarandache[ i ] THEN
count +:= 1;
s last := i;
p last := count;
IF count <= 25 THEN
print( ( " ", whole( i, 0 ) ) )
ELIF count = 100 THEN
s100 := i
ELIF count = 1000 THEN
s1000 := i
FI
FI
OD;
print( ( newline ) );
print( ( "100th Smarandache prime: ", whole( s100, 0 ), newline ) );
print( ( "1000th Smarandache prime: ", whole( s1000, 0 ), newline ) );
print( ( "Largest Smarandache prime under "
, whole( prime max, 0 )
, ": "
, whole( s last, 0 )
, " (Smarandache prime "
, whole( p last, 0 )
, ")"
, newline
)
)
END

View file

@ -0,0 +1,38 @@
# syntax: GAWK -f SMARANDACHE_PRIME-DIGITAL_SEQUENCE.AWK
BEGIN {
limit = 25
printf("1-%d:",limit)
while (1) {
if (is_prime(++n)) {
if (all_digits_prime(n) == 1) {
if (++count <= limit) {
printf(" %d",n)
}
if (count == 100) {
printf("\n%d: %d\n",count,n)
break
}
}
}
}
exit(0)
}
function all_digits_prime(n, i) {
for (i=1; i<=length(n); i++) {
if (!is_prime(substr(n,i,1))) {
return(0)
}
}
return(1)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}

View file

@ -0,0 +1,81 @@
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

View file

@ -0,0 +1,9 @@
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]

View file

@ -0,0 +1,31 @@
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

View file

@ -0,0 +1,69 @@
#include <iostream>
#include <cstdint>
using integer = uint32_t;
integer next_prime_digit_number(integer n) {
if (n == 0)
return 2;
switch (n % 10) {
case 2:
return n + 1;
case 3:
case 5:
return n + 2;
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 = 1000000000;
integer n = 0, max = 0;
std::cout << "First 25 SPDS primes:\n";
for (int i = 0; n < limit; ) {
n = next_prime_digit_number(n);
if (!is_prime(n))
continue;
if (i < 25) {
if (i > 0)
std::cout << ' ';
std::cout << 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 << "Largest SPDS prime less than " << limit << ": " << max << '\n';
return 0;
}

View file

@ -0,0 +1,71 @@
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
typedef uint32_t integer;
integer next_prime_digit_number(integer n) {
if (n == 0)
return 2;
switch (n % 10) {
case 2:
return n + 1;
case 3:
case 5:
return n + 2;
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;
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];
}
}
}
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))
continue;
if (i < 25) {
if (i > 0)
printf(" ");
printf("%'u", 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);
return 0;
}

View file

@ -0,0 +1,43 @@
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;

View file

@ -0,0 +1,5 @@
// 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)
spds [2;3;5;7] |> Seq.take 25 |> Seq.iter(printfn "%d")
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)

View file

@ -0,0 +1,18 @@
USING: combinators.short-circuit io lists lists.lazy math
math.parser math.primes prettyprint sequences ;
IN: rosetta-code.smarandache-naive
: smarandache? ( n -- ? )
{
[ number>string string>digits [ prime? ] all? ]
[ prime? ]
} 1&& ;
: smarandache ( -- list ) 1 lfrom [ smarandache? ] lfilter ;
: smarandache-demo ( -- )
"First 25 members of the Smarandache prime-digital sequence:"
print 25 smarandache ltake list>array .
"100th member: " write smarandache 99 [ cdr ] times car . ;
MAIN: smarandache-demo

View file

@ -0,0 +1,55 @@
USING: combinators generalizations io kernel math math.functions
math.primes prettyprint sequences ;
IN: rosetta-code.smarandache
! Observations:
! * For 2-digit numbers and higher, only 3 and 7 are viable in
! the ones place.
! * Only 2, 3, 5, and 7 are viable anywhere else.
! * It is possible to use this information to drastically
! reduce the amount of numbers to check for primality.
! * For instance, by these rules we can tell that the next
! potential Smarandache prime digital after 777 is 2223.
: next-one ( n -- n' ) 3 = 7 3 ? ; inline
: next-ten ( n -- n' )
{ { 2 [ 3 ] } { 3 [ 5 ] } { 5 [ 7 ] } [ drop 2 ] } case ;
: inc ( seq quot: ( n -- n' ) -- seq' )
[ 0 ] 2dip [ change-nth ] curry keep ; inline
: inc1 ( seq -- seq' ) [ next-one ] inc ;
: inc10 ( seq -- seq' ) [ next-ten ] inc ;
: inc-all ( seq -- seq' )
inc1 [ zero? not [ next-ten ] when ] V{ } map-index-as ;
: carry ( seq -- seq' )
dup [ 7 = not ] find drop {
{ 0 [ inc1 ] }
{ f [ inc-all 2 suffix! ] }
[ cut [ inc-all ] [ inc10 ] bi* append! ]
} case ;
: digits>integer ( seq -- n ) [ 10 swap ^ * ] map-index sum ;
: next-smarandache ( seq -- seq' )
[ digits>integer prime? ] [ carry dup ] do until ;
: .sm ( seq -- ) <reversed> [ pprint ] each nl ;
: first25 ( -- )
2 3 5 7 [ . ] 4 napply V{ 7 } clone
21 [ next-smarandache dup .sm ] times drop ;
: nth-smarandache ( n -- )
4 - V{ 7 } clone swap [ next-smarandache ] times .sm ;
: smarandache-demo ( -- )
"First 25 members of the Smarandache prime-digital sequence:"
print first25 nl { 100 1000 10000 100000 } [
dup pprint "th member: " write nth-smarandache
] each ;
MAIN: smarandache-demo

View file

@ -0,0 +1,48 @@
: 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

View file

@ -0,0 +1,26 @@
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

View file

@ -0,0 +1,54 @@
package main
import (
"fmt"
"math/big"
)
var b = new(big.Int)
func isSPDSPrime(n uint64) bool {
nn := n
for nn > 0 {
r := nn % 10
if r != 2 && r != 3 && r != 5 && r != 7 {
return false
}
nn /= 10
}
b.SetUint64(n)
if b.ProbablyPrime(0) { // 100% accurate up to 2 ^ 64
return true
}
return false
}
func listSPDSPrimes(startFrom, countFrom, countTo uint64, printOne bool) uint64 {
count := countFrom
for n := startFrom; ; n += 2 {
if isSPDSPrime(n) {
count++
if !printOne {
fmt.Printf("%2d. %d\n", count, n)
}
if count == countTo {
if printOne {
fmt.Println(n)
}
return n
}
}
}
}
func main() {
fmt.Println("The first 25 terms of the Smarandache prime-digital sequence are:")
fmt.Println(" 1. 2")
n := listSPDSPrimes(3, 1, 25, false)
fmt.Println("\nHigher terms:")
indices := []uint64{25, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000}
for i := 1; i < len(indices); i++ {
fmt.Printf("%6d. ", indices[i])
n = listSPDSPrimes(n+2, indices[i-1], indices[i], true)
}
}

View file

@ -0,0 +1,88 @@
package main
import (
"fmt"
"math/big"
)
type B2357 []byte
var bi = new(big.Int)
func isSPDSPrime(b B2357) bool {
bi.SetString(string(b), 10)
return bi.ProbablyPrime(0) // 100% accurate up to 2 ^ 64
}
func listSPDSPrimes(startFrom B2357, countFrom, countTo uint64, printOne bool) B2357 {
count := countFrom
n := startFrom
for {
if isSPDSPrime(n) {
count++
if !printOne {
fmt.Printf("%2d. %s\n", count, string(n))
}
if count == countTo {
if printOne {
fmt.Println(string(n))
}
return n
}
}
if printOne {
n = n.AddTwo()
} else {
n = n.AddOne()
}
}
}
func incDigit(digit byte) byte {
switch digit {
case '2':
return '3'
case '3':
return '5'
case '5':
return '7'
default:
return '9' // say
}
}
func (b B2357) AddOne() B2357 {
le := len(b)
b[le-1] = incDigit(b[le-1])
for i := le - 1; i >= 0; i-- {
if b[i] < '9' {
break
} else if i > 0 {
b[i] = '2'
b[i-1] = incDigit(b[i-1])
} else {
b[0] = '2'
nb := make(B2357, le+1)
copy(nb[1:], b)
nb[0] = '2'
return nb
}
}
return b
}
func (b B2357) AddTwo() B2357 {
return b.AddOne().AddOne()
}
func main() {
fmt.Println("The first 25 terms of the Smarandache prime-digital sequence are:")
n := listSPDSPrimes(B2357{'2'}, 0, 4, false)
n = listSPDSPrimes(n.AddOne(), 4, 25, false)
fmt.Println("\nHigher terms:")
indices := []uint64{25, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000}
for i := 1; i < len(indices); i++ {
fmt.Printf("%6d. ", indices[i])
n = listSPDSPrimes(n.AddTwo(), indices[i-1], indices[i], true)
}
}

View file

@ -0,0 +1,29 @@
{-# LANGUAGE NumericUnderscores #-}
import Control.Monad (guard)
import Math.NumberTheory.Primes.Testing (isPrime)
import Data.List.Split (chunksOf)
import Data.List (intercalate)
import Text.Printf (printf)
smarandache :: [Integer]
smarandache = [2,3,5,7] <> s [2,3,5,7] >>= \x -> guard (isPrime x) >> [x]
where s xs = r <> s r where r = xs >>= \x -> [x*10+2, x*10+3, x*10+5, x*10+7]
nextSPDSTerms :: [Int] -> [(String, String)]
nextSPDSTerms = go 1 smarandache
where
go _ _ [] = []
go c (x:xs) terms
| c `elem` terms = (commas c, commas x) : go nextCount xs (tail terms)
| otherwise = go nextCount xs terms
where nextCount = succ c
commas :: Show a => a -> String
commas = reverse . intercalate "," . chunksOf 3 . reverse . show
main :: IO ()
main = do
printf "The first 25 SPDS:\n%s\n\n" $ f smarandache
mapM_ (uncurry (printf "The %9sth SPDS: %15s\n")) $
nextSPDSTerms [100, 1_000, 10_000, 100_000, 1_000_000]
where f = show . take 25

View file

@ -0,0 +1,82 @@
public class SmarandachePrimeDigitalSequence {
public static void main(String[] args) {
long s = getNextSmarandache(7);
System.out.printf("First 25 Smarandache prime-digital sequence numbers:%n2 3 5 7 ");
for ( int count = 1 ; count <= 21 ; s = getNextSmarandache(s) ) {
if ( isPrime(s) ) {
System.out.printf("%d ", s);
count++;
}
}
System.out.printf("%n%n");
for (int i = 2 ; i <=5 ; i++ ) {
long n = (long) Math.pow(10, i);
System.out.printf("%,dth Smarandache prime-digital sequence number = %d%n", n, getSmarandachePrime(n));
}
}
private static final long getSmarandachePrime(long n) {
if ( n < 10 ) {
switch ((int) n) {
case 1: return 2;
case 2: return 3;
case 3: return 5;
case 4: return 7;
}
}
long s = getNextSmarandache(7);
long result = 0;
for ( int count = 1 ; count <= n-4 ; s = getNextSmarandache(s) ) {
if ( isPrime(s) ) {
count++;
result = s;
}
}
return result;
}
private static final boolean isPrime(long test) {
if ( test % 2 == 0 ) return false;
for ( long i = 3 ; i <= Math.sqrt(test) ; i += 2 ) {
if ( test % i == 0 ) {
return false;
}
}
return true;
}
private static long getNextSmarandache(long n) {
// If 3, next is 7
if ( n % 10 == 3 ) {
return n+4;
}
long retVal = n-4;
// Last digit 7. k = largest position from right where we have a 7.
int k = 0;
while ( n % 10 == 7 ) {
k++;
n /= 10;
}
// Determine first digit from right where digit != 7.
long digit = n % 10;
// Digit is 2, 3, or 5. 3-2 = 1, 5-3 = 2, 7-5 = 2, so digit = 2, coefficient = 1, otherwise 2.
long coeff = (digit == 2 ? 1 : 2);
// Compute next value
retVal += coeff * Math.pow(10, k);
// Subtract values for digit = 7.
while ( k > 1 ) {
retVal -= 5 * Math.pow(10, k-1);
k--;
}
// Even works for 777..777 --> 2222...223
return retVal;
}
}

View file

@ -0,0 +1,28 @@
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))"

View file

@ -0,0 +1,25 @@
using Combinatorics, Primes
combodigits(len) = sort!(unique(map(y -> join(y, ""), with_replacement_combinations("2357", len))))
function getprimes(N, maxdigits=9)
ret = [2, 3, 5, 7]
perms = Int[]
for i in 1:maxdigits-1, combo in combodigits(i), perm in permutations(combo)
n = parse(Int64, String(perm)) * 10
push!(perms, n + 3, n + 7)
end
for perm in sort!(perms)
if isprime(perm) && !(perm in ret)
push!(ret, perm)
if length(ret) >= N
return ret
end
end
end
end
const v = getprimes(10000)
println("The first 25 Smarandache primes are: ", v[1:25])
println("The 100th Smarandache prime is: ", v[100])
println("The 10000th Smarandache prime is: ", v[10000])

View file

@ -0,0 +1,18 @@
-- 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
-- SIEVE:
local sieve, S = {}, 50000
for i = 2,S do sieve[i]=true end
for i = 2,S do if sieve[i] then for j=i*i,S,i do sieve[j]=nil end end end
-- TASKS:
local digs, cans, spds, N = {2,3,5,7}, T{0}, T{}, 100
while #spds < N do
local c = cans:remove(1)
for _,d in ipairs(digs) do cans:insert(c*10+d) end
if sieve[c] then spds:insert(c) end
end
print("1-25 : " .. spds:firstn(25):concat(" "))
print("100th: " .. spds[100])

View file

@ -0,0 +1,5 @@
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]]

View file

@ -0,0 +1,51 @@
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

View file

@ -0,0 +1,72 @@
program Smarandache;
uses
sysutils,primsieve;// http://rosettacode.org/wiki/Extensible_prime_generator#Pascal
const
Digits : array[0..3] of Uint32 = (2,3,5,7);
var
i,j,pot10,DgtLimit,n,DgtCnt,v,cnt,LastPrime,Limit : NativeUint;
procedure Check(n:NativeUint);
var
p : NativeUint;
Begin
p := LastPrime;
while p< n do
p := nextprime;
if p = n then
begin
inc(cnt);
IF (cnt <= 25) then
Begin
IF cnt = 25 then
Begin
writeln(n);
Limit := 100;
end
else
Write(n,',');
end
else
IF cnt = Limit then
Begin
Writeln(cnt:9,n:16);
Limit *=10;
if Limit > 10000 then
HALT;
end;
end;
LastPrime := p;
end;
Begin
Limit := 25;
LastPrime:=1;
//Creating the numbers not the best way but all upto 11 digits take 0.05s
//here only 9 digits
i := 0;
pot10 := 1;
DgtLimit := 1;
v := 4;
repeat
repeat
j := i;
DgtCnt := 0;
pot10 := 1;
n := 0;
repeat
n += pot10*Digits[j MOD 4];
j := j DIV 4;
pot10 *=10;
inc(DgtCnt);
until DgtCnt = DgtLimit;
Check(n);
inc(i);
until i=v;
//one more digit
v *=4;
i :=0;
inc(DgtLimit);
until DgtLimit= 12;
end.

View file

@ -0,0 +1,23 @@
use strict;
use warnings;
use feature 'say';
use feature 'state';
use ntheory qw<is_prime>;
use Lingua::EN::Numbers qw(num2en_ordinal);
my @prime_digits = <2 3 5 7>;
my @spds = grep { is_prime($_) && /^[@{[join '',@prime_digits]}]+$/ } 1..100;
my @p = map { $_+3, $_+7 } map { 10*$_ } @prime_digits;
while ($#spds < 100_000) {
state $o++;
my $oom = 10**(1+$o);
my @q;
for my $l (@prime_digits) {
push @q, map { $l*$oom + $_ } @p;
}
push @spds, grep { is_prime($_) } @p = @q;
}
say 'Smarandache prime-digitals:';
printf "%22s: %s\n", ucfirst(num2en_ordinal($_)), $spds[$_-1] for 1..25, 100, 1000, 10_000, 100_000;

View file

@ -0,0 +1,52 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
<span style="color: #004080;">atom</span> <span style="color: #000000;">nxt_candidate</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">23</span>
<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>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<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>
<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>
<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>
<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>
<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>
<span style="color: #000000;">spds</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">nxt_candidate</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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>
<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>
<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>
<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>
<span style="color: #000000;">adx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<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>
<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>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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>
<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>
<span style="color: #000080;font-style:italic;">-- (this is eg 777, by now 223 carry 1, -&gt; 2223)</span>
<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>
<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>
<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>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">populate_spds</span><span style="color: #0000FF;">(</span><span style="color: #000000;">25</span><span style="color: #0000FF;">)</span>
<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>
<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>
<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>
<span style="color: #000000;">populate_spds</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<!--

View file

@ -0,0 +1,45 @@
def divisors(n):
divs = [1]
for ii in range(2, int(n ** 0.5) + 3):
if n % ii == 0:
divs.append(ii)
divs.append(int(n / ii))
divs.append(n)
return list(set(divs))
def is_prime(n):
return len(divisors(n)) == 2
def digit_check(n):
if len(str(n))<2:
return True
else:
for digit in str(n):
if not is_prime(int(digit)):
return False
return True
def sequence(max_n=None):
ii = 0
n = 0
while True:
ii += 1
if is_prime(ii):
if max_n is not None:
if n>max_n:
break
if digit_check(ii):
n += 1
yield ii
if __name__ == '__main__':
generator = sequence(100)
for index, item in zip(range(1, 16), generator):
print(index, item)
for index, item in zip(range(16, 100), generator):
pass
print(100, generator.__next__())

View file

@ -0,0 +1,16 @@
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
100 33223

View file

@ -0,0 +1,19 @@
[ 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

View file

@ -0,0 +1,29 @@
[ 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

View file

@ -0,0 +1,30 @@
/*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.*/
if q='' then q= 100 1000 /* " " " " " " */
say 'listing the first' n "SPDS primes═══"
call spds n
do i=1 for words(q)+1; y=word(q, i); if y=='' | y=="," then iterate
say
say 'listing the last of ' y "SPDS primes═══"
call spds -y
end /*i*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
spds: parse arg x 1 ox; x= abs(x) /*obtain the limit to be used for list.*/
c= 0 /*C number of SPDS primes found so far*/
#= 0 /*# number of primes found so far*/
do j=1 by 2 while c<x; z= j /*start: 1st even prime, then use odd. */
if z==1 then z= 2 /*handle the even prime (special case) */
/* [↓] divide by the primes. ___ */
do k=2 to # while k*k<=z /*divide Z with all primes ≤ √ Z */
if z//@.k==0 then iterate j /*÷ by prev. prime? ¬prime ___ */
end /*j*/ /* [↑] only divide up to √ Z */
#= # + 1; @.#= z /*bump the prime count; assign prime #*/
if verify(z, 2357)>0 then iterate j /*Digits ¬prime? Then skip this prime.*/
c= c + 1 /*bump the number of SPDS primes found.*/
if ox<0 then iterate /*don't display it, display the last #.*/
say right(z, 21) /*maybe display this prime ──► terminal*/
end /*j*/ /* [↑] only display N number of primes*/
if ox<0 then say right(z, 21) /*display one (the last) SPDS prime. */
return

View file

@ -0,0 +1,9 @@
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;

View file

@ -0,0 +1,28 @@
load "stdlib.ring"
see "First 25 Smarandache primes:" + nl + nl
num = 0
limit = 26
limit100 = 100
for n = 1 to 34000
flag = 0
nStr = string(n)
for x = 1 to len(nStr)
nx = number(nStr[x])
if isprime(n) and isprime(nx)
flag = flag + 1
else
exit
ok
next
if flag = len(nStr)
num = num + 1
if num < limit
see "" + n + " "
ok
if num = limit100
see nl + nl + "100th Smarandache prime: " + n + nl
ok
ok
next

View file

@ -0,0 +1,17 @@
require "prime"
smarandache = Enumerator.new do|y|
prime_digits = [2,3,5,7]
prime_digits.each{|pr| y << pr} # yield the below-tens
(1..).each do |n|
prime_digits.repeated_permutation(n).each do |perm|
c = perm.join.to_i * 10
y << c + 3 if (c+3).prime?
y << c + 7 if (c+7).prime?
end
end
end
seq = smarandache.take(100)
p seq.first(25)
p seq.last

View file

@ -0,0 +1,73 @@
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);
}
}

View file

@ -0,0 +1,6 @@
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)

View file

@ -0,0 +1,68 @@
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)")

View file

@ -0,0 +1,29 @@
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])")

View file

@ -0,0 +1,36 @@
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);
]

View file

@ -0,0 +1,34 @@
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

View file

@ -0,0 +1,8 @@
var [const] BI=Import("zklBigNum"); // libGMP
spds:=Walker.zero().tweak(fcn(ps){
var [const] nps=T(0,0,1,1,0,1,0,1,0,0); // 2,3,5,7
p:=ps.nextPrime().toInt();
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)));

View file

@ -0,0 +1,6 @@
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)));

View file

@ -0,0 +1,5 @@
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);