Smarandache prime-digital sequence: Difference between revisions
m
syntax highlighting fixup automation
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: note use of 'ntheory' module) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 19:
{{trans|Python}}
<
V divs = [1]
L(ii) 2 .< Int(n ^ 0.5) + 3
Line 59:
print(item, end' ‘ ’)
print()
print(‘Hundredth SPDS prime: ’seq[99])</
{{out}}
Line 70:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<
BYTE FUNC IsZero(REAL POINTER a)
Line 150:
a==+1
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Smarandache_prime-digital_sequence.png Screenshot from Atari 8-bit computer]
Line 163:
Uses a sieve to find primes. Requires --heap 256m for Algol 68G.
<br>Uses the optimisations of the Factor, Phix, etc. samples.
<
# digits are all primes #
# Uses the observations that the final digit of 2 or more digit Smarandache #
Line 264:
)
)
END</
{{out}}
<pre>
Line 275:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SMARANDACHE_PRIME-DIGITAL_SEQUENCE.AWK
BEGIN {
Line 314:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 323:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<
dim smar(100)
smar[1] = 2
Line 353:
end while
return True
end function</
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
Line 360:
=={{header|C}}==
{{trans|C++}}
<
#include <stdbool.h>
#include <stdint.h>
Line 430:
printf("Largest SPDS prime less than %'u: %'u\n", limit, max);
return 0;
}</
{{out}}
Line 443:
=={{header|C++}}==
<
#include <cstdint>
Line 511:
std::cout << "Largest SPDS prime less than " << limit << ": " << max << '\n';
return 0;
}</
{{out}}
Line 525:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<
// 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 531:
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>
{{out}}
<pre>
Line 567:
=={{header|Factor}}==
===Naive===
<
math.parser math.primes prettyprint sequences ;
IN: rosetta-code.smarandache-naive
Line 584:
"100th member: " write smarandache 99 [ cdr ] times car . ;
MAIN: smarandache-demo</
{{out}}
<pre>
Line 619:
===Optimized===
<
math.primes prettyprint sequences ;
IN: rosetta-code.smarandache
Line 673:
] each ;
MAIN: smarandache-demo</
{{out}}
<pre>
Line 710:
=={{header|Forth}}==
<
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
Line 757:
1000 spds_nth . cr
bye</
{{out}}
Line 768:
=={{header|FreeBASIC}}==
<
function isprime( n as ulongint ) as boolean
if n < 2 then return false
Line 794:
print count, smar(count)
end if
wend</
{{out}}
<pre>
Line 834:
=={{header|Go}}==
===Basic===
<
import (
Line 887:
n = listSPDSPrimes(n+2, indices[i-1], indices[i], true)
}
}</
{{out}}
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.
<
import (
Line 1,021:
n = listSPDSPrimes(n.AddTwo(), indices[i-1], indices[i], true)
}
}</
{{out}}
Line 1,030:
=={{header|Haskell}}==
Using the optimized approach of generated numbers from prime digits and testing for primality.
<
import Control.Monad (guard)
import Math.NumberTheory.Primes.Testing (isPrime)
Line 1,058:
mapM_ (uncurry (printf "The %9sth SPDS: %15s\n")) $
nextSPDSTerms [100, 1_000, 10_000, 100_000, 1_000_000]
where f = show . take 25</
{{out}}
<pre>The first 25 SPDS:
Line 1,094:
=={{header|Java}}==
Generate next in sequence directly from previous, inspired by previous solutions.
<
public class SmarandachePrimeDigitalSequence {
Line 1,177:
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,196:
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime` as used here.
<
# Output: a naively constructed stream of candidate strings of length >= 1
def Smarandache_candidates:
Line 1,223:
# jq counts from 0 so:
"\nThe hundredth: \(nth(99; Smarandache_primes))"</
{{out}}
<pre>
Line 1,238:
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.
<
using Combinatorics, Primes
Line 1,264:
println("The 100th Smarandache prime is: ", v[100])
println("The 10000th Smarandache prime is: ", v[10000])
</
<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,272:
=={{header|Lua}}==
<
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,289:
end
print("1-25 : " .. spds:firstn(25):concat(" "))
print("100th: " .. spds[100])</
{{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
Line 1,295:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
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]]</
{{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,305:
=={{header|Nim}}==
<
const N = 35_000
Line 1,355:
inc count
if count == 100:
echo "The 100th SPDS is: ", n</
{{out}}
Line 1,365:
uses [[http://rosettacode.org/wiki/Extensible_prime_generator#Pascal:Extensible_prime_generator]]<BR>
Simple Brute force.Testing for prime takes most of the time.
<
uses
Line 1,436:
inc(DgtLimit);
until DgtLimit= 12;
end.</
{{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,446:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,468:
say 'Smarandache prime-digitals:';
printf "%22s: %s\n", ucfirst(num2en_ordinal($_)), $spds[$_-1] for 1..25, 100, 1000, 10_000, 100_000;</
{{out}}
<pre> First: 2
Line 1,511:
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.
<!--<
<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>
Line 1,562:
<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>
<!--</
{{out}}
<pre>
Line 1,583:
=={{header|Python}}==
<syntaxhighlight lang="python">
def divisors(n):
divs = [1]
Line 1,629:
pass
print(100, generator.__next__())
</syntaxhighlight>
<b>Output</b>
<syntaxhighlight lang="python">
1 2
2 3
Line 1,649:
15 353
100 33223
</syntaxhighlight>
=={{header|Quackery}}==
Line 1,657:
===Naive===
<
[ 10 /mod
[ table 1 1 0 0 1 0 1 0 1 1 ]
Line 1,675:
25 split swap echo
cr cr
-1 peek echo</
{{out}}
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.
<
[ 5 /mod 0 = while
dip [ 5 * 1+ ]
Line 1,715:
25 split swap echo
cr cr
-1 peek echo</
{{out}}
Line 1,726:
(formerly Perl 6)
{{libheader|ntheory}}
<syntaxhighlight lang="raku"
use ntheory:from<Perl5> <:all>;
Line 1,734:
say 'Smarandache prime-digitals:';
printf "%22s: %s\n", ordinal(1+$_).tclc, comma $spds[$_] for flat ^25, 99, 999, 9999, 99999;</
{{out}}
<pre>Smarandache prime-digitals:
Line 1,769:
=={{header|REXX}}==
The prime number generator has been simplified and very little optimization was included.
<
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,798:
end /*j*/ /* [↑] only display N number of primes*/
if ox<0 then say right(z, 21) /*display one (the last) SPDS prime. */
return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,836:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,865:
ok
next
</syntaxhighlight>
{{Out}}
<pre>
Line 1,876:
=={{header|Rust}}==
<
if n < 2 {
return false;
Line 1,948:
println!("Largest SPDS prime less than {}: {}", limit, p);
}
}</
{{out}}
Line 1,962:
=={{header|Ruby}}==
Attaching 3 and 7 to permutations of 2,3,5 and 7
<
smarandache = Enumerator.new do|y|
Line 1,979:
p seq.first(25)
p seq.last
</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]
Line 1,987:
=={{header|Sidef}}==
<
n.is_prime && n.digits.all { .is_prime }
}
say is_prime_digital.first(25).join(',')
say is_prime_digital.nth(100)</
{{out}}
<pre>
Line 2,001:
=={{header|Swift}}==
{{trans|C++}}
<
if number < 2 {
return false
Line 2,068:
max = n
}
print("Largest SPDS prime less than \(limit): \(max)")</
{{out}}
Line 2,083:
{{libheader|Wren-math}}
Simple brute-force approach.
<
var limit = 1000
Line 2,111:
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])")</
{{out}}
Line 2,124:
=={{header|XPL0}}==
<
int N, I;
[if N <= 2 then return N = 2;
Line 2,159:
];
Text(0, "^m^j1000th: "); IntOut(0, N); CrLf(0);
]</
{{out}}
Line 2,170:
=={{header|Yabasic}}==
{{trans|Ring}}
<
limit = 26
limit100 = 100
Line 2,203:
wend
return True
end sub</
{{out}}
<pre>Igual que la entrada de Ring.</pre>
Line 2,214:
[[Extensible prime generator#zkl]] could be used instead.
<
spds:=Walker.zero().tweak(fcn(ps){
Line 2,221:
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)));</
Or
<
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)));</
<
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);</
{{out}}
<pre>
|