Extra primes: Difference between revisions

m
Added Easylang
m (Added Easylang)
 
(12 intermediate revisions by 9 users not shown)
Line 20:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">V limit = 10'000
 
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
Line 43:
I is_extra_prime(n)
i++
print(‘#4’.format(n), end' I i % 9 == 0 {"\n"} E ‘ ’)</langsyntaxhighlight>
 
{{out}}
Line 55:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE Func IsExtraPrime(INT i BYTE ARRAY primes)
Line 91:
OD
PrintF("%E%EThere are %I extra primes",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Extra_primes.png Screenshot from Atari 8-bit computer]
Line 102:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Extra_Primes is
Line 176:
New_Line;
Put_Line (Count'Image & " extra primes.");
end Extra_Primes;</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 23 223 227 337
Line 184:
7253 7523 7723 7727
36 extra primes.</pre>
 
=={{header|ALGOL 68}}==
Based on the Algol W sample.
<syntaxhighlight lang="algol68">
BEGIN # find extra primes - numbers whose digits are prime and whose #
# digit sum is prime #
# the digits can only be 2, 3, 5, 7 #
# other than 1 digit numbers, the first three digits #
# can be 0, 2, 3, 5, 7 and the final digit can only be 3 and 7 #
# which means there are at most 5^3 * 2 = 250 possible numbers #
# so we will use trial division for primality testing #
# returns TRUE if n is prime, FALSE otherwise - uses trial division #
PROC is prime = ( INT n )BOOL:
IF n < 3 THEN n = 2
ELIF n MOD 3 = 0 THEN n = 3
ELIF NOT ODD n THEN FALSE
ELSE
BOOL is a prime := TRUE;
FOR f FROM 5 BY 2 WHILE f * f <= n AND is a prime DO
is a prime := n MOD f /= 0
OD;
is a prime
FI # is prime # ;
# first four numbers ) i.e.the 1 digit primes ) as a special case #
print( ( " 2 3 5 7" ) );
INT count := 4;
# 2, 3 and 5 digit numberrs #
INT d1 := 0;
TO 5 DO
INT d2 := 0;
TO 5 DO
IF ( d1 + d2 ) = 0 OR d2 /= 0 THEN
INT d3 := 2;
TO 4 DO
FOR d4 FROM 3 BY 4 TO 7 DO
INT sum = d1 + d2 + d3 + d4;
INT n = ( ( ( ( ( d1 * 10 ) + d2 ) * 10 ) + d3 ) * 10 ) + d4;
IF is prime( sum ) AND is prime( n ) THEN
# found a prime whose prime digits sum to a prime #
print( ( " ", whole( n, -4 ) ) );
IF ( count +:= 1 ) MOD 12 = 0 THEN print( ( newline ) ) FI
FI
OD;
d3 +:= 1;
IF d3 = 4 OR d3 = 6 THEN d3 +:= 1 FI
OD
FI;
d2 +:= 1;
IF d2 = 1 OR d2 = 4 OR d2 = 6 THEN d2 +:= 1 FI
OD;
d1 +:= 1;
IF d1 = 1 OR d1 = 4 OR d1 = 6 THEN d1 +:= 1 FI
OD
END
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 23 223 227 337 353 373 557 577
733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323
3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727
</pre>
 
=={{header|ALGOL W}}==
As the digits can only be 2, 3, 5 or 7 (see the Wren sample) we can easily generate the candidates for the sequence.
<langsyntaxhighlight lang="algolw">begin
% find extra primes - numbers whose digits are prime and whose %
% digit sum is prime %
Line 231 ⟶ 292:
end for_d1
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 240 ⟶ 301:
 
=={{header|APL}}==
<langsyntaxhighlight APLlang="apl">extraPrimes←{
pd←0 2 3 5 7
ds←↓⍉(∧⌿ds∊pd)/ds←10(⊥⍣¯1)1↓⍳⍵
Line 252 ⟶ 313:
}2
(sieve[ns]∧sieve[ss])/ns
}</langsyntaxhighlight>
 
{{out}}
 
<langsyntaxhighlight APLlang="apl"> extraPrimes 10000
2 3 5 7 23 27 223 227 333 337 353 373 377 533 553 557 577 733 737 757
773 777 2223 2227 2333 2353 2357 2377 2533 2537 2557 2573 2577
2737 2753 2757 2773 2777 3233 3253 3257 3277 3323 3523 3527 3727
5233 5237 5257 5273 5277 5323 5327 5527 5723 5727 7237 7253 7257
7273 7277 7327 7523 7527 7723 7727</langsyntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">extraPrime?: function [n]->
all? @[
prime? n
prime? sum digits n
every? digits n => prime?
]
 
extraPrimesBelow10K: select 1..10000 => extraPrime?
 
loop split.every: 9 extraPrimesBelow10K 'x ->
print map x 's -> pad to :string s 5</syntaxhighlight>
 
{{out}}
 
<pre> 2 3 5 7 23 223 227 337 353
373 557 577 733 757 773 2333 2357 2377
2557 2753 2777 3253 3257 3323 3527 3727 5233
5237 5273 5323 5527 7237 7253 7523 7723 7727</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f EXTRA_PRIMES.AWK
BEGIN {
Line 295 ⟶ 376:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 337 ⟶ 418:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z: DIM S(7777),D(4): DATA 0,2,3,5,7
15 FOR I=0 TO 4: READ D(I): NEXT
20 FOR I=2 TO SQR(7777)
Line 352 ⟶ 433:
120 NEXT
130 NEXT
140 NEXT</langsyntaxhighlight>
 
{{out}}
Line 366 ⟶ 447:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
Line 433 ⟶ 514:
printf("%'u: %'u\n", n-i, extra_primes[(n-i) % last]);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 489 ⟶ 570:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 555 ⟶ 636:
std::cout << n-i << ": " << extra_primes[(n-i) % last] << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 611 ⟶ 692:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
const MAXPRIME := 7777;
 
Line 664 ⟶ 745:
end loop;
i1 := i1 + 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 704 ⟶ 785:
7723
7727</pre>
 
 
 
 
 
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.stdio;
 
int nextPrimeDigitNumber(int n) {
Line 774 ⟶ 859:
}
writeln;
}</langsyntaxhighlight>
{{out}}
<pre>Extra primes under 10000
Line 813 ⟶ 898:
35: 7723
36: 7727</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses Delphi string to examine to sum and test digits.
 
<syntaxhighlight lang="Delphi">
 
function IsPrime(N: integer): boolean;
{Optimised prime test - about 40% faster than the naive approach}
var I,Stop: integer;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (i + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
function IsExtraPrime(N: integer): boolean;
{Check if 1) The number is prime}
{2) All the digits in the number is prime}
{3) The sum of all digits is prime}
var S: string;
var I,Sum,D: integer;
begin
Result:=False;
if not IsPrime(N) then exit;
Sum:=0;
S:=IntToStr(N);
for I:=1 to Length(S) do
begin
D:=byte(S[I])-$30;
if not IsPrime(D) then exit;
Sum:=Sum+D;
end;
Result:=IsPrime(Sum);
end;
 
 
procedure ShowExtraPrimes(Memo: TMemo);
{Show all extra-primes less than 10,000}
var I: integer;
var Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=1 to 10000 do
if IsExtraPrime(I) then
begin
Inc(Cnt);
S:=S+Format('%5d',[I]);
if (Cnt mod 9)=0 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 23 223 227 337 353
373 557 577 733 757 773 2333 2357 2377
2557 2753 2777 3253 3257 3323 3527 3727 5233
5237 5273 5323 5527 7237 7253 7523 7723 7727
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
if num mod 2 = 0 and num > 2
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
func digprim n .
while n > 0
d = n mod 10
if d < 2 or d = 4 or d = 6 or d >= 8
return 0
.
sum += d
n = n div 10
.
return isprim sum
.
p = 2
while p < 10000
if isprim p = 1 and digprim p = 1
write p & " "
.
p += 1
.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727
</pre>
 
=={{header|F_Sharp|F#}}==
===The Function===
This task uses [[Permutations/Derangements#F.23]]
<langsyntaxhighlight lang="fsharp">
// Extra Primes. Nigel Galloway: January 9th., 2021
let izXprime g=let rec fN n g=match n with 0L->isPrime64 g |_->if isPrime64(n%10L) then fN (n/10L) (n%10L+g) else false in fN g 0L
</syntaxhighlight>
</lang>
===The tasks===
; Extra primes below 10,000
<langsyntaxhighlight lang="fsharp">
primes64() |> Seq.filter izXprime |> Seq.takeWhile((>) 10000L) |> Seq.iteri(printfn "%3d->%d")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 866 ⟶ 1,064:
</pre>
; Last 10 Extra primes below 1,000,000,000
<langsyntaxhighlight lang="fsharp">
primes64()|>Seq.takeWhile((>)1000000000L)|>Seq.rev|>Seq.filter izXprime|>Seq.take 10|>Seq.rev|>Seq.iter(printf "%d ");printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 874 ⟶ 1,072:
</pre>
; Last 10 Extra primes below 10,000,000,000
<langsyntaxhighlight lang="fsharp">
primes64()|>Seq.skipWhile((>)7770000000L)|>Seq.takeWhile((>)7777777777L)|>List.ofSeq|>List.filter izXprime|>List.rev|>List.take 10|>List.rev|>List.iter(printf "%d ");printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 885 ⟶ 1,083:
{{trans|Wren}}
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.functions math.primes
sequences sequences.extras ;
 
Line 907 ⟶ 1,105:
[ sum prime? ] filter
[ digits>number ] [ prime? ] map-filter
[ 1 + swap "%2d: %4d\n" printf ] each-index</langsyntaxhighlight>
{{out}}
<pre style="height: 45ex">
Line 950 ⟶ 1,148:
 
=={{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 1,009 ⟶ 1,207:
." Number of extra primes under 100000000: " . cr
 
bye</langsyntaxhighlight>
 
{{out}}
Line 1,054 ⟶ 1,252:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
dim as uinteger p(0 to 4) = {0,2,3,5,7}, d3, d2, d1, d0, pd1, pd2, pd3, pd0
 
Line 1,080 ⟶ 1,278:
next d1
next d2
next d3</langsyntaxhighlight>
{{out}}
<pre>
Line 1,121 ⟶ 1,319:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">select[primes[2,10000], {|n| isPrime[sum[integerDigits[n]]] and isSubset[toSet[integerDigits[n]], new set[2,3,5,7]]}]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,129 ⟶ 1,327:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,194 ⟶ 1,392:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,235 ⟶ 1,433:
35: 7723
36: 7727
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">
import Data.Char ( digitToInt )
 
isPrime :: Int -> Bool
isPrime n
|n < 2 = False
|otherwise = null $ filter (\i -> mod n i == 0 ) [2 .. root]
where
root :: Int
root = floor $ sqrt $ fromIntegral n
 
condition :: Int -> Bool
condition n = isPrime n && all isPrime digits && isPrime ( sum digits )
where
digits :: [Int]
digits = map digitToInt ( show n )
 
solution :: [Int]
solution = filter condition [1..9999]</syntaxhighlight>
 
{{out}}
<pre>
[2,3,5,7,23,223,227,337,353,373,557,577,733,757,773,2333,2357,2377,2557,2753,2777,3253,3257,3323,3527,3727,5233,5237,5273,5323,5527,7237,7253,7523,7723,7727]
</pre>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">exprimes =: (] #~ *./@(1&p:)@(+/ , ])@(10 #.^:_1 ])"0)@(i.&.(p:^:_1))</langsyntaxhighlight>
 
{{out}}
 
<langsyntaxhighlight lang="j"> exprimes 10000
2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Go}}
<langsyntaxhighlight lang="java">public class ExtraPrimes {
private static int nextPrimeDigitNumber(int n) {
if (n == 0) {
Line 1,314 ⟶ 1,538:
System.out.println();
}
}</langsyntaxhighlight>
{{out}}
<pre>Extra primes under 10000:
Line 1,361 ⟶ 1,585:
 
One small point of interest is the declaration of $p before the inner function that references it.
<syntaxhighlight lang="jq">
<lang jq>
# Input: the maximum width
# Output: a stream
Line 1,378 ⟶ 1,602:
 
# The task:
4|extraprimes</langsyntaxhighlight>
{{out}}
<pre>
Line 1,393 ⟶ 1,617:
</pre>
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function extraprimes(maxlen)
Line 1,405 ⟶ 1,629:
 
extraprimes(4)
</langsyntaxhighlight>{{out}}
<pre>
2
Line 1,447 ⟶ 1,671:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">private fun nextPrimeDigitNumber(n: Int): Int {
return if (n == 0) {
2
Line 1,508 ⟶ 1,732:
}
println()
}</langsyntaxhighlight>
{{out}}
<pre>Extra primes under 10000:
Line 1,550 ⟶ 1,774:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function next_prime_digit_number(n)
if n == 0 then
return 2
Line 1,630 ⟶ 1,854:
print(string.format("%d: %d", n - i, extra_primes[(n - i) % last]))
i = i - 1
end</langsyntaxhighlight>
{{out}}
<pre>Extra primes under 10000:
Line 1,681 ⟶ 1,905:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
BOOLEAN PRIME
DIMENSION PRIME(7777)
Line 1,707 ⟶ 1,931:
X CONTINUE
 
END OF PROGRAM </langsyntaxhighlight>
 
{{out}}
Line 1,750 ⟶ 1,974:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Select[Range[10000], PrimeQ[#] && AllTrue[IntegerDigits[#], PrimeQ] &]</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,2333,2357,2377,2557,2753,2777,3253,3257,3323,3373,3527,3533,3557,3727,3733,5227,5233,5237,5273,5323,5333,5527,5557,5573,5737,7237,7253,7333,7523,7537,7573,7577,7723,7727,7753,7757}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils
 
const N = 10_000
Line 1,794 ⟶ 2,018:
for i, p in result:
stdout.write ($p).align(4)
stdout.write if (i + 1) mod 9 == 0: '\n' else: ' '</langsyntaxhighlight>
 
{{out}}
Line 1,802 ⟶ 2,026:
2557 2753 2777 3253 3257 3323 3527 3727 5233
5237 5273 5323 5527 7237 7253 7523 7723 7727</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
using simple circular buffer for last n solutions.With crossing 10th order of magnitude like in Raku.
<syntaxhighlight lang="pascal">program SpecialPrimes;
// modified smarandache
{$IFDEF FPC}{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
const
Digits : array[0..3] of Uint32 = (2,3,5,7);
 
var
//circular buffer
Last64 : array[0..63] of Uint64;
cnt,Limit : NativeUint;
LastIdx: Int32;
procedure OutLast(i:Int32);
var
idx: Int32;
begin
idx := LastIdx-i;
If idx < Low(Last64) then
idx += High(Last64)+1;
For i := i downto 1 do
begin
write(Last64[idx]:12);
inc(idx);
if idx > High(Last64) then
idx := Low(Last64);
end;
writeln;
end;
 
function isSmlPrime64(n:UInt32):boolean;inline;
//n must be >=0 and <=180 = 20 times digit 9, uses 80x86 BIT TEST
begin
EXIT(n in [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,
79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,
157,163,167,173,179])
end;
 
function isPrime(n:UInt64):boolean;
const
deltaWheel : array[0..7] of byte =( 2, 6, 4, 2, 4, 2, 4, 6);
var
p : NativeUint;
WheelIdx : Int32;
begin
if n < 180 then
EXIT(isSmlPrime64(n));
 
result := false;
 
if n mod 2 = 0 then EXIT;
if n mod 3 = 0 then EXIT;
if n mod 5 = 0 then EXIT;
 
p := 1;
WheelIdx := High(deltaWheel);
repeat
inc(p,deltaWheel[WheelIdx]);
if p*p > n then
BREAK;
if n mod p = 0 then
EXIT;
dec(WheelIdx);
IF WheelIdx< Low(deltaWheel) then
wheelIdx := High(deltaWheel);
until false;
result := true;
end;
 
procedure Check(n:NativeUint);
 
Begin
if isPrime(n) then
begin
Last64[LastIdx] := n;
inc(LastIdx);
If LastIdx>High(Last64) then
LastIdx := Low(Last64);
inc(cnt);
IF (n < 10000) then
Begin
write(n:5,',');
if cnt mod 10 = 0 then
writeln;
if cnt = 36 then
writeln;
end
else
IF n > Limit then
Begin
OutLast(7);
Limit *=10;
end;
end;
end;
 
var
i,j,pot10,DgtLimit,n,DgtCnt,v : NativeUint;
dgt,
dgtsum : Int32;
Begin
Limit := 100000;
cnt := 0;
LastIdx := 0;
//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;
dgtsum := 0;
repeat
dgt := Digits[j MOD 4];
dgtsum += dgt;
n += pot10*Dgt;
j := j DIV 4;
pot10 *=10;
inc(DgtCnt);
until DgtCnt = DgtLimit;
if isPrime(dgtsum) then Check(n);
inc(i);
until i=v;
//one more digit
v *=4;
i :=0;
inc(DgtLimit);
until DgtLimit= 12;
inc(LastIdx);
OutLast(7);
writeln('count: ',cnt);
end.</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
2, 3, 5, 7, 23, 223, 227, 337, 353, 373,
557, 577, 733, 757, 773, 2333, 2357, 2377, 2557, 2753,
2777, 3253, 3257, 3323, 3527, 3727, 5233, 5237, 5273, 5323,
5527, 7237, 7253, 7523, 7723, 7727,
75577 75773 77377 77557 77573 77773 222337
772573 773273 773723 775237 775273 777277 2222333
7775737 7775753 7777337 7777537 7777573 7777753 22222223
77755523 77757257 77757523 77773277 77773723 77777327 222222227
777775373 777775553 777775577 777777227 777777577 777777773 2222222377
7777772773 7777773257 7777773277 7777775273 7777777237 7777777327 22222222223
77777757773 77777773537 77777773757 77777775553 77777777533 77777777573 {{77777272733 63 places before last}}
count: 107308
Real time: 46.241 s CPU share: 99.38 %
@home: Real time: 8.615 s maybe much faster div ( Ryzen 5600G 4.4 Ghz vs Xeon 2.3 Ghz)
count < 1E
1E5, E6, E7, E8, E9, E10, E11
89,222,718,2498,9058,32189,107308
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,814 ⟶ 2,201:
is_prime(vecsum(todigits($_))) and /^[2357]+$/ and $str .= sprintf '%-5d', $_;
} 1e4;
say $str =~ s/.{1,80}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,824 ⟶ 2,211:
=={{header|Phix}}==
Minor reworking of [[Numbers_with_prime_digits_whose_sum_is_13#iterative|Numbers_with_prime_digits_whose_sum_is_13#Phix#iterative]]
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1_000_000_000</span><span style="color: #0000FF;">,</span>
<span style="color: #000080;font-style:italic;">--constant limit = 10_000,</span>
Line 1,857 ⟶ 2,244:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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,876 ⟶ 2,263:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import *
from functools import reduce
 
Line 1,913 ⟶ 2,300:
for n in takewhile(lambda n: n < 10000, extra_primes()):
print(n)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,953 ⟶ 2,340:
7723
7727</pre>
 
=={{header|Quackery}}==
 
<code>eratosthenes</code> and <code>isprime</code> are defines at [[Sieve of Eratosthenes#Quckery]].
 
<syntaxhighlight lang="Quackery"> [ [] swap
[ 10 /mod
rot join swap
dup 0 = until ]
drop ] is digits ( n --> [ )
 
[ 0 swap witheach + ] is sum ( [ --> n )
 
10000 eratosthenes
[]
10000 times
[ i^ isprime not if done
true i^ digits
tuck witheach
[ isprime and
dup not if conclude ]
not iff drop done
sum isprime
if [ i^ join ] ]
echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727 ]</pre>
 
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 1,970 ⟶ 2,387:
(and (prime? p) (prime-sum-of-prime-digits? p)))
 
(displayln (filter extra-prime? (range 10000)))</langsyntaxhighlight>
 
{{out}}
Line 1,979 ⟶ 2,396:
For the time being, (Doctor?), I'm going to assume that the task is really "Sequence of primes with every digit a prime and the sum of the digits a prime". Outputting my own take on a reasonable display of results, compact and easily doable but exercising it a bit.
 
<syntaxhighlight lang="raku" perl6line>my @ppp = lazy flat 2, 3, 5, 7, 23, grep { .is-prime && .comb.sum.is-prime },
flat (2..*).map: { flat ([X~] (2, 3, 5, 7) xx $_) X~ (3, 7) };
 
put 'Terms < 10,000: '.fmt('%34s'), @ppp[^(@ppp.first: * > 1e4, :k)];
put '991st through 1000th: '.fmt('%34s'), @ppp[990 .. 999];
put 'Crossing 10th order of magnitude: ', @ppp[9055..9060];</langsyntaxhighlight>
{{out}}
<pre> Terms < 10,000: 2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727
Line 1,994 ⟶ 2,411:
 
If the limit is negative,&nbsp; the list of primes found isn't shown,&nbsp; but the count of primes found is always shown.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds & shows all primes whose digits are prime and the digits sum to a prime*/
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 10000 /*Not specified? Then use the default.*/
Line 2,042 ⟶ 2,459:
#=#+1; @.#= j; sq.#= j*j; !.j= 1 /*bump number of primes; assign prime#.*/
end /*j*/
hP= @.#; return # /*hP: is the highest prime generated. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
 
Line 2,077 ⟶ 2,494:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,119 ⟶ 2,536:
return 0
ok
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,160 ⟶ 2,577:
</pre>
 
=={{header|RPL}}==
≪ DUP →STR "014689" → n nstring baddigits
≪ 1 CF 0
1 nstring SIZE '''FOR''' j
nstring j DUP SUB
'''IF''' baddigits OVER POS '''THEN''' 1 SF '''END'''
STR→ + '''NEXT'''
'''IF''' 1 FC?
'''THEN IF''' <span style="color:blue">PRIM?</span> '''THEN''' n <span style="color:blue">PRIM?</span> '''ELSE''' 0 '''END'''
'''ELSE''' DROP 0 '''END'''
≫ ≫ ‘<span style="color:blue">XTRA?</span>’ STO
≪ { } 1 10000 '''FOR''' n '''IF''' n <span style="color:blue">XTRA?</span> '''THEN''' n + '''END NEXT''' ≫ EVAL
<code>PRIM?</code> is defined at [[Primality by trial division#RPL|Primality by trial division]] or can be replaced by <code>ISPRIME?</code> when using a RPL 2003+ version.
{{out}}
<pre>
1: { 2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727 }
</pre>
=={{header|Ruby}}==
{{trans|Java}}
<langsyntaxhighlight lang="ruby">def nextPrimeDigitNumber(n)
if n == 0 then
return 2
Line 2,225 ⟶ 2,660:
end
end
print "\n"</langsyntaxhighlight>
{{out}}
<pre>Extra primes under 10000:
Line 2,266 ⟶ 2,701:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 2,320 ⟶ 2,755:
println!("{}: {}", n - i, extra_primes[(n - i) % last]);
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,377 ⟶ 2,812:
=={{header|Sidef}}==
Simple solution:
<langsyntaxhighlight lang="ruby">say 1e4.primes.grep { .digits.all { .is_prime } && .sumdigits.is_prime }</langsyntaxhighlight>
{{out}}
<pre>
Line 2,384 ⟶ 2,819:
 
Generate such primes from digits (faster):
<langsyntaxhighlight lang="ruby">func extra_primes(upto, base = 10) {
 
upto = prev_prime(upto+1)
Line 2,417 ⟶ 2,852:
say "\nLast 10 extra primes <= #{n.commify}:"
say extra_primes(n).last(10).join(' ')
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,428 ⟶ 2,863:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
let wheel = [4,2,4,2,4,6,2,6]
Line 2,524 ⟶ 2,959:
for i in stride(from: last - 1, through: 0, by: -1) {
print("\(commatize(n - i)): \(commatize(extraPrimes[(n - i) % last]))")
}</langsyntaxhighlight>
 
{{out}}
Line 2,550 ⟶ 2,985:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var digits = [2, 3, 5, 7] // the only digits which are primes
Line 2,582 ⟶ 3,017:
Fmt.print("$2d: $4d", count, cand[0])
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,626 ⟶ 3,061:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 2,652 ⟶ 3,087:
[IntOut(0, P); CrLf(0)];
];
]</langsyntaxhighlight>
 
{{out}}
1,962

edits