Smarandache prime-digital sequence: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Raku}}: note use of 'ntheory' module)
m (syntax highlighting fixup automation)
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 70:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
BYTE FUNC IsZero(REAL POINTER a)
Line 150:
a==+1
OD
RETURN</langsyntaxhighlight>
{{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.
<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 264:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 275:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SMARANDACHE_PRIME-DIGITAL_SEQUENCE.AWK
BEGIN {
Line 314:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 323:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">arraybase 1
dim smar(100)
smar[1] = 2
Line 353:
end while
return True
end function</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
Line 360:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
Line 430:
printf("Largest SPDS prime less than %'u: %'u\n", limit, max);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 443:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
 
Line 511:
std::cout << "Largest SPDS prime less than " << limit << ": " << max << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 525:
=={{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 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>
</lang>
{{out}}
<pre>
Line 567:
=={{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 584:
"100th member: " write smarandache 99 [ cdr ] times car . ;
 
MAIN: smarandache-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 619:
 
===Optimized===
<langsyntaxhighlight lang="factor">USING: combinators generalizations io kernel math math.functions
math.primes prettyprint sequences ;
IN: rosetta-code.smarandache
Line 673:
] each ;
 
MAIN: smarandache-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 710:
 
=={{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 757:
1000 spds_nth . cr
 
bye</langsyntaxhighlight>
 
{{out}}
Line 768:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
function isprime( n as ulongint ) as boolean
if n < 2 then return false
Line 794:
print count, smar(count)
end if
wend</langsyntaxhighlight>
{{out}}
<pre>
Line 834:
=={{header|Go}}==
===Basic===
<langsyntaxhighlight lang="go">package main
 
import (
Line 887:
n = listSPDSPrimes(n+2, indices[i-1], indices[i], true)
}
}</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,021:
n = listSPDSPrimes(n.AddTwo(), indices[i-1], indices[i], true)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,030:
=={{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 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</langsyntaxhighlight>
{{out}}
<pre>The first 25 SPDS:
Line 1,094:
=={{header|Java}}==
Generate next in sequence directly from previous, inspired by previous solutions.
<langsyntaxhighlight lang="java">
public class SmarandachePrimeDigitalSequence {
 
Line 1,177:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,196:
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime` as used here.
<langsyntaxhighlight lang="jq">def Smarandache_primes:
# 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))"</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="julia">
using Combinatorics, Primes
 
Line 1,264:
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,272:
 
=={{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,289:
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
Line 1,295:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="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]]</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,305:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strformat, strutils
 
const N = 35_000
Line 1,355:
inc count
if count == 100:
echo "The 100th SPDS is: ", n</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="pascal">program Smarandache;
 
uses
Line 1,436:
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,446:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
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;</langsyntaxhighlight>
{{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.
<!--<langsyntaxhighlight Phixlang="phix">(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>
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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,583:
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
def divisors(n):
divs = [1]
Line 1,629:
pass
print(100, generator.__next__())
</syntaxhighlight>
</lang>
 
<b>Output</b>
<syntaxhighlight lang="python">
<lang Python>
1 2
2 3
Line 1,649:
15 353
100 33223
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
Line 1,657:
===Naive===
 
<langsyntaxhighlight Quackerylang="quackery"> [ true swap
[ 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</langsyntaxhighlight>
 
{{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.
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 over
[ 5 /mod 0 = while
dip [ 5 * 1+ ]
Line 1,715:
25 split swap echo
cr cr
-1 peek echo</langsyntaxhighlight>
 
{{out}}
Line 1,726:
(formerly Perl 6)
{{libheader|ntheory}}
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
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;</langsyntaxhighlight>
{{out}}
<pre>Smarandache prime-digitals:
Line 1,769:
=={{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,798:
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,836:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,865:
ok
next
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,876:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn is_prime(n: u32) -> bool {
if n < 2 {
return false;
Line 1,948:
println!("Largest SPDS prime less than {}: {}", limit, p);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,962:
=={{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,979:
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,987:
 
=={{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 2,001:
=={{header|Swift}}==
{{trans|C++}}
<langsyntaxhighlight lang="swift">func isPrime(number: Int) -> Bool {
if number < 2 {
return false
Line 2,068:
max = n
}
print("Largest SPDS prime less than \(limit): \(max)")</langsyntaxhighlight>
 
{{out}}
Line 2,083:
{{libheader|Wren-math}}
Simple brute-force approach.
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
 
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])")</langsyntaxhighlight>
 
{{out}}
Line 2,124:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 2,159:
];
Text(0, "^m^j1000th: "); IntOut(0, N); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 2,170:
=={{header|Yabasic}}==
{{trans|Ring}}
<langsyntaxhighlight lang="yabasic">num = 0
limit = 26
limit100 = 100
Line 2,203:
wend
return True
end sub</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de Ring.</pre>
Line 2,214:
 
[[Extensible prime generator#zkl]] could be used instead.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
 
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)));</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>
10,327

edits