Smarandache prime-digital sequence: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: note use of 'ntheory' module) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 19: | Line 19: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F divisors(n) |
||
V divs = [1] |
V divs = [1] |
||
L(ii) 2 .< Int(n ^ 0.5) + 3 |
L(ii) 2 .< Int(n ^ 0.5) + 3 |
||
Line 59: | Line 59: | ||
print(item, end' ‘ ’) |
print(item, end' ‘ ’) |
||
print() |
print() |
||
print(‘Hundredth SPDS prime: ’seq[99])</ |
print(‘Hundredth SPDS prime: ’seq[99])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 70: | Line 70: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit |
||
BYTE FUNC IsZero(REAL POINTER a) |
BYTE FUNC IsZero(REAL POINTER a) |
||
Line 150: | Line 150: | ||
a==+1 |
a==+1 |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Smarandache_prime-digital_sequence.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Smarandache_prime-digital_sequence.png Screenshot from Atari 8-bit computer] |
||
Line 163: | Line 163: | ||
Uses a sieve to find primes. Requires --heap 256m for Algol 68G. |
Uses a sieve to find primes. Requires --heap 256m for Algol 68G. |
||
<br>Uses the optimisations of the Factor, Phix, etc. samples. |
<br>Uses the optimisations of the Factor, Phix, etc. samples. |
||
< |
<syntaxhighlight lang="algol68"># find elements of the Smarandache prime-digital sequence - primes whose # |
||
# digits are all primes # |
# digits are all primes # |
||
# Uses the observations that the final digit of 2 or more digit Smarandache # |
# Uses the observations that the final digit of 2 or more digit Smarandache # |
||
Line 264: | Line 264: | ||
) |
) |
||
) |
) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 275: | Line 275: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SMARANDACHE_PRIME-DIGITAL_SEQUENCE.AWK |
# syntax: GAWK -f SMARANDACHE_PRIME-DIGITAL_SEQUENCE.AWK |
||
BEGIN { |
BEGIN { |
||
Line 314: | Line 314: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 323: | Line 323: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="freebasic">arraybase 1 |
||
dim smar(100) |
dim smar(100) |
||
smar[1] = 2 |
smar[1] = 2 |
||
Line 353: | Line 353: | ||
end while |
end while |
||
return True |
return True |
||
end function</ |
end function</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Igual que la entrada de FreeBASIC.</pre> |
<pre>Igual que la entrada de FreeBASIC.</pre> |
||
Line 360: | Line 360: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="c">#include <locale.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
#include <stdint.h> |
#include <stdint.h> |
||
Line 430: | Line 430: | ||
printf("Largest SPDS prime less than %'u: %'u\n", limit, max); |
printf("Largest SPDS prime less than %'u: %'u\n", limit, max); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 443: | Line 443: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <cstdint> |
#include <cstdint> |
||
Line 511: | Line 511: | ||
std::cout << "Largest SPDS prime less than " << limit << ": " << max << '\n'; |
std::cout << "Largest SPDS prime less than " << limit << ": " << max << '\n'; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 525: | Line 525: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Generate Smarandache prime-digital sequence. Nigel Galloway: May 31st., 2019 |
// 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) |
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 531: | Line 531: | ||
printfn "\n\n100th item of this sequence is %d" (spds [2;3;5;7] |> Seq.item 99) |
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) |
printfn "1000th item of this sequence is %d" (spds [2;3;5;7] |> Seq.item 999) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 567: | Line 567: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
===Naive=== |
===Naive=== |
||
< |
<syntaxhighlight lang="factor">USING: combinators.short-circuit io lists lists.lazy math |
||
math.parser math.primes prettyprint sequences ; |
math.parser math.primes prettyprint sequences ; |
||
IN: rosetta-code.smarandache-naive |
IN: rosetta-code.smarandache-naive |
||
Line 584: | Line 584: | ||
"100th member: " write smarandache 99 [ cdr ] times car . ; |
"100th member: " write smarandache 99 [ cdr ] times car . ; |
||
MAIN: smarandache-demo</ |
MAIN: smarandache-demo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 619: | Line 619: | ||
===Optimized=== |
===Optimized=== |
||
< |
<syntaxhighlight lang="factor">USING: combinators generalizations io kernel math math.functions |
||
math.primes prettyprint sequences ; |
math.primes prettyprint sequences ; |
||
IN: rosetta-code.smarandache |
IN: rosetta-code.smarandache |
||
Line 673: | Line 673: | ||
] each ; |
] each ; |
||
MAIN: smarandache-demo</ |
MAIN: smarandache-demo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 710: | Line 710: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: is_prime? ( n -- flag ) |
||
dup 2 < if drop false exit then |
dup 2 < if drop false exit then |
||
dup 2 mod 0= if 2 = exit then |
dup 2 mod 0= if 2 = exit then |
||
Line 757: | Line 757: | ||
1000 spds_nth . cr |
1000 spds_nth . cr |
||
bye</ |
bye</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 768: | Line 768: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
function isprime( n as ulongint ) as boolean |
function isprime( n as ulongint ) as boolean |
||
if n < 2 then return false |
if n < 2 then return false |
||
Line 794: | Line 794: | ||
print count, smar(count) |
print count, smar(count) |
||
end if |
end if |
||
wend</ |
wend</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 834: | Line 834: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
===Basic=== |
===Basic=== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 887: | Line 887: | ||
n = listSPDSPrimes(n+2, indices[i-1], indices[i], true) |
n = listSPDSPrimes(n+2, indices[i-1], indices[i], true) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 934: | Line 934: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,021: | Line 1,021: | ||
n = listSPDSPrimes(n.AddTwo(), indices[i-1], indices[i], true) |
n = listSPDSPrimes(n.AddTwo(), indices[i-1], indices[i], true) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,030: | Line 1,030: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Using the optimized approach of generated numbers from prime digits and testing for primality. |
Using the optimized approach of generated numbers from prime digits and testing for primality. |
||
< |
<syntaxhighlight lang="haskell">{-# LANGUAGE NumericUnderscores #-} |
||
import Control.Monad (guard) |
import Control.Monad (guard) |
||
import Math.NumberTheory.Primes.Testing (isPrime) |
import Math.NumberTheory.Primes.Testing (isPrime) |
||
Line 1,058: | Line 1,058: | ||
mapM_ (uncurry (printf "The %9sth SPDS: %15s\n")) $ |
mapM_ (uncurry (printf "The %9sth SPDS: %15s\n")) $ |
||
nextSPDSTerms [100, 1_000, 10_000, 100_000, 1_000_000] |
nextSPDSTerms [100, 1_000, 10_000, 100_000, 1_000_000] |
||
where f = show . take 25</ |
where f = show . take 25</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The first 25 SPDS: |
<pre>The first 25 SPDS: |
||
Line 1,094: | Line 1,094: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
Generate next in sequence directly from previous, inspired by previous solutions. |
Generate next in sequence directly from previous, inspired by previous solutions. |
||
< |
<syntaxhighlight lang="java"> |
||
public class SmarandachePrimeDigitalSequence { |
public class SmarandachePrimeDigitalSequence { |
||
Line 1,177: | Line 1,177: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,196: | Line 1,196: | ||
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime` as used here. |
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 |
# Output: a naively constructed stream of candidate strings of length >= 1 |
||
def Smarandache_candidates: |
def Smarandache_candidates: |
||
Line 1,223: | Line 1,223: | ||
# jq counts from 0 so: |
# jq counts from 0 so: |
||
"\nThe hundredth: \(nth(99; Smarandache_primes))"</ |
"\nThe hundredth: \(nth(99; Smarandache_primes))"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,238: | Line 1,238: | ||
add numbers that end in 3 or 7 and that only contain 2, 3, 5, and 7. This |
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. |
can be done via permutations of combinations with repetition. |
||
< |
<syntaxhighlight lang="julia"> |
||
using Combinatorics, Primes |
using Combinatorics, Primes |
||
Line 1,264: | Line 1,264: | ||
println("The 100th Smarandache prime is: ", v[100]) |
println("The 100th Smarandache prime is: ", v[100]) |
||
println("The 10000th Smarandache prime is: ", v[10000]) |
println("The 10000th Smarandache prime is: ", v[10000]) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<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] |
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,272: | Line 1,272: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- FUNCS: |
||
local function T(t) return setmetatable(t, {__index=table}) end |
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 |
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,289: | Line 1,289: | ||
end |
end |
||
print("1-25 : " .. spds:firstn(25):concat(" ")) |
print("1-25 : " .. spds:firstn(25):concat(" ")) |
||
print("100th: " .. spds[100])</ |
print("100th: " .. spds[100])</syntaxhighlight> |
||
{{out}} |
{{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 |
<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 |
||
Line 1,295: | Line 1,295: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[SmarandachePrimeQ] |
||
SmarandachePrimeQ[n_Integer] := MatchQ[IntegerDigits[n], {(2 | 3 | 5 | 7) ..}] \[And] PrimeQ[n] |
SmarandachePrimeQ[n_Integer] := MatchQ[IntegerDigits[n], {(2 | 3 | 5 | 7) ..}] \[And] PrimeQ[n] |
||
s = Select[Range[10^5], SmarandachePrimeQ]; |
s = Select[Range[10^5], SmarandachePrimeQ]; |
||
Take[s, UpTo[25]] |
Take[s, UpTo[25]] |
||
s[[100]]</ |
s[[100]]</syntaxhighlight> |
||
{{out}} |
{{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} |
<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,305: | Line 1,305: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import math, strformat, strutils |
||
const N = 35_000 |
const N = 35_000 |
||
Line 1,355: | Line 1,355: | ||
inc count |
inc count |
||
if count == 100: |
if count == 100: |
||
echo "The 100th SPDS is: ", n</ |
echo "The 100th SPDS is: ", n</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,365: | Line 1,365: | ||
uses [[http://rosettacode.org/wiki/Extensible_prime_generator#Pascal:Extensible_prime_generator]]<BR> |
uses [[http://rosettacode.org/wiki/Extensible_prime_generator#Pascal:Extensible_prime_generator]]<BR> |
||
Simple Brute force.Testing for prime takes most of the time. |
Simple Brute force.Testing for prime takes most of the time. |
||
< |
<syntaxhighlight lang="pascal">program Smarandache; |
||
uses |
uses |
||
Line 1,436: | Line 1,436: | ||
inc(DgtLimit); |
inc(DgtLimit); |
||
until DgtLimit= 12; |
until DgtLimit= 12; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{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 |
<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,446: | Line 1,446: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,468: | Line 1,468: | ||
say 'Smarandache prime-digitals:'; |
say 'Smarandache prime-digitals:'; |
||
printf "%22s: %s\n", ucfirst(num2en_ordinal($_)), $spds[$_-1] for 1..25, 100, 1000, 10_000, 100_000;</ |
printf "%22s: %s\n", ucfirst(num2en_ordinal($_)), $spds[$_-1] for 1..25, 100, 1000, 10_000, 100_000;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> First: 2 |
<pre> First: 2 |
||
Line 1,511: | Line 1,511: | ||
but because of the massive gaps (eg between 777,777,777 and 2,222,222,223) it proved much faster |
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. |
to test each candidate for primality individually. Timings below show just how much this improves things. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span> |
||
Line 1,562: | Line 1,562: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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> |
<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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,583: | Line 1,583: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
def divisors(n): |
def divisors(n): |
||
divs = [1] |
divs = [1] |
||
Line 1,629: | Line 1,629: | ||
pass |
pass |
||
print(100, generator.__next__()) |
print(100, generator.__next__()) |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output</b> |
<b>Output</b> |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
1 2 |
1 2 |
||
2 3 |
2 3 |
||
Line 1,649: | Line 1,649: | ||
15 353 |
15 353 |
||
100 33223 |
100 33223 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
Line 1,657: | Line 1,657: | ||
===Naive=== |
===Naive=== |
||
< |
<syntaxhighlight lang="quackery"> [ true swap |
||
[ 10 /mod |
[ 10 /mod |
||
[ table 1 1 0 0 1 0 1 0 1 1 ] |
[ table 1 1 0 0 1 0 1 0 1 1 ] |
||
Line 1,675: | Line 1,675: | ||
25 split swap echo |
25 split swap echo |
||
cr cr |
cr cr |
||
-1 peek echo</ |
-1 peek echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,687: | Line 1,687: | ||
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. |
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 |
[ 5 /mod 0 = while |
||
dip [ 5 * 1+ ] |
dip [ 5 * 1+ ] |
||
Line 1,715: | Line 1,715: | ||
25 split swap echo |
25 split swap echo |
||
cr cr |
cr cr |
||
-1 peek echo</ |
-1 peek echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,726: | Line 1,726: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
<lang |
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers; |
||
use ntheory:from<Perl5> <:all>; |
use ntheory:from<Perl5> <:all>; |
||
Line 1,734: | Line 1,734: | ||
say 'Smarandache prime-digitals:'; |
say 'Smarandache prime-digitals:'; |
||
printf "%22s: %s\n", ordinal(1+$_).tclc, comma $spds[$_] for flat ^25, 99, 999, 9999, 99999;</ |
printf "%22s: %s\n", ordinal(1+$_).tclc, comma $spds[$_] for flat ^25, 99, 999, 9999, 99999;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Smarandache prime-digitals: |
<pre>Smarandache prime-digitals: |
||
Line 1,769: | Line 1,769: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
The prime number generator has been simplified and very little optimization was included. |
The prime number generator has been simplified and very little optimization was included. |
||
< |
<syntaxhighlight 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*/ |
parse arg n q /*get optional number of primes to find*/ |
||
if n=='' | n=="," then n= 25 /*Not specified? Then use the default.*/ |
if n=='' | n=="," then n= 25 /*Not specified? Then use the default.*/ |
||
Line 1,798: | Line 1,798: | ||
end /*j*/ /* [↑] only display N number of primes*/ |
end /*j*/ /* [↑] only display N number of primes*/ |
||
if ox<0 then say right(z, 21) /*display one (the last) SPDS prime. */ |
if ox<0 then say right(z, 21) /*display one (the last) SPDS prime. */ |
||
return</ |
return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,836: | Line 1,836: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 1,865: | Line 1,865: | ||
ok |
ok |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,876: | Line 1,876: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn is_prime(n: u32) -> bool { |
||
if n < 2 { |
if n < 2 { |
||
return false; |
return false; |
||
Line 1,948: | Line 1,948: | ||
println!("Largest SPDS prime less than {}: {}", limit, p); |
println!("Largest SPDS prime less than {}: {}", limit, p); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,962: | Line 1,962: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Attaching 3 and 7 to permutations of 2,3,5 and 7 |
Attaching 3 and 7 to permutations of 2,3,5 and 7 |
||
< |
<syntaxhighlight lang="ruby">require "prime" |
||
smarandache = Enumerator.new do|y| |
smarandache = Enumerator.new do|y| |
||
Line 1,979: | Line 1,979: | ||
p seq.first(25) |
p seq.first(25) |
||
p seq.last |
p seq.last |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{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] |
<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,987: | Line 1,987: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func is_prime_digital(n) { |
||
n.is_prime && n.digits.all { .is_prime } |
n.is_prime && n.digits.all { .is_prime } |
||
} |
} |
||
say is_prime_digital.first(25).join(',') |
say is_prime_digital.first(25).join(',') |
||
say is_prime_digital.nth(100)</ |
say is_prime_digital.nth(100)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,001: | Line 2,001: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="swift">func isPrime(number: Int) -> Bool { |
||
if number < 2 { |
if number < 2 { |
||
return false |
return false |
||
Line 2,068: | Line 2,068: | ||
max = n |
max = n |
||
} |
} |
||
print("Largest SPDS prime less than \(limit): \(max)")</ |
print("Largest SPDS prime less than \(limit): \(max)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,083: | Line 2,083: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
Simple brute-force approach. |
Simple brute-force approach. |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
var limit = 1000 |
var limit = 1000 |
||
Line 2,111: | Line 2,111: | ||
System.print(spds.take(25).toList) |
System.print(spds.take(25).toList) |
||
System.print("\nThe 100th SPDS prime is %(spds[99])") |
System.print("\nThe 100th SPDS prime is %(spds[99])") |
||
System.print("\nThe 1,000th SPDS prime is %(spds[999])")</ |
System.print("\nThe 1,000th SPDS prime is %(spds[999])")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,124: | Line 2,124: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime |
||
int N, I; |
int N, I; |
||
[if N <= 2 then return N = 2; |
[if N <= 2 then return N = 2; |
||
Line 2,159: | Line 2,159: | ||
]; |
]; |
||
Text(0, "^m^j1000th: "); IntOut(0, N); CrLf(0); |
Text(0, "^m^j1000th: "); IntOut(0, N); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,170: | Line 2,170: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|Ring}} |
{{trans|Ring}} |
||
< |
<syntaxhighlight lang="yabasic">num = 0 |
||
limit = 26 |
limit = 26 |
||
limit100 = 100 |
limit100 = 100 |
||
Line 2,203: | Line 2,203: | ||
wend |
wend |
||
return True |
return True |
||
end sub</ |
end sub</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Igual que la entrada de Ring.</pre> |
<pre>Igual que la entrada de Ring.</pre> |
||
Line 2,214: | Line 2,214: | ||
[[Extensible prime generator#zkl]] could be used instead. |
[[Extensible prime generator#zkl]] could be used instead. |
||
< |
<syntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP |
||
spds:=Walker.zero().tweak(fcn(ps){ |
spds:=Walker.zero().tweak(fcn(ps){ |
||
Line 2,221: | Line 2,221: | ||
if(p.split().filter( fcn(n){ 0==nps[n] }) ) return(Void.Skip); |
if(p.split().filter( fcn(n){ 0==nps[n] }) ) return(Void.Skip); |
||
p // 733 --> (7,3,3) --> () --> good, 29 --> (2,9) --> (9) --> bad |
p // 733 --> (7,3,3) --> () --> good, 29 --> (2,9) --> (9) --> bad |
||
}.fp(BI(1)));</ |
}.fp(BI(1)));</syntaxhighlight> |
||
Or |
Or |
||
< |
<syntaxhighlight lang="zkl">spds:=Walker.zero().tweak(fcn(ps){ |
||
var [const] nps="014689".inCommon; |
var [const] nps="014689".inCommon; |
||
p:=ps.nextPrime().toInt(); |
p:=ps.nextPrime().toInt(); |
||
if(nps(p.toString())) return(Void.Skip); |
if(nps(p.toString())) return(Void.Skip); |
||
p // 733 --> "" --> good, 29 --> "9" --> bad |
p // 733 --> "" --> good, 29 --> "9" --> bad |
||
}.fp(BI(1)));</ |
}.fp(BI(1)));</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">println("The first 25 terms of the Smarandache prime-digital sequence are:"); |
||
spds.walk(25).concat(",").println(); |
spds.walk(25).concat(",").println(); |
||
println("The hundredth term of the sequence is: ",spds.drop(100-25).value); |
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);</ |
println("1000th item of this sequence is : ",spds.drop(1_000-spds.n).value);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |